2012-12-31 7 views
0

모델 완료 될 때까지 기다립니다 entry.js.coffeebackbone.js + 커피 스크립트 - 아약스가

class Raffler.Models.Entry extends Backbone.Model 

    url: '/api/entries/12345' 

라우터 : entries_router.js.cofee를

routes: 
'entries/:id': 'show' 

show: (id) -> 
    @model = new Raffler.Models.Entry() 
    @model.fetch() 
    view1 = new Raffler.Views.Profile(model: @model) 
    $("#profile_container").html(view1.render().el) 

보기 : profile.js.coffee

class Raffler.Views.Profile extends Backbone.View 
    template: JST['entries/profile'] 

render: -> 
    $(@el).html(@template(entry: @model)) 
    this 

템플릿 : profile.jst.eco

<div class="profile_desc"> 
    <table> 
    <tr> 
     <td valign="top" class="desc_heading">about: </td> 
     <td> 
     <%= @model.get('aboutMe') %> 
     </td> 
    </tr> 
    </table> 
</div> 

JSON 응답 :

{"aboutMe":"I am a proud Ruby Developer} 

나는 데이터를 가져 오는 AJAX 호출을 볼 수 있습니다. 하지만 그 전에 뷰 파일을 렌더링 같아요. 내가 완료 모델 아약스를 기다릴 수있는 방법

TypeError: this.model is undefined 
    __out.push(__sanitize(this.model.get('aboutMe'))); 

:

파이어 버그에서 오류가있어?

+0

보기 및 템플릿, 응답을 편집하여 추가했습니다. 그래, 그 컴파일 및 오류 던졌습니다 –

답변

1

인출하기 전에, 당신은 다음과 같이 콜백 함수 뭔가를 지정해야합니다

show: (id) -> 
    @model = new Raffler.Models.Entry() 
    @model.on 'sync', => @create_profile_view @model 
    @model.fetch() 

create_profile_view: (model) -> 
    view1 = new Raffler.Views.Profile(model: model) 
    $("#profile_container").html(view1.render().el) 

당신은 당신이 원하는 다양한 이벤트를 (http://backbonejs.org/#Events)를 사용할 수 있습니다.

P. 렌더링 로직을 추출하고 뷰를 가져 오는 것을 선호합니다.

+0

오류가 발생했습니다 : 나는 @ entry.get ('aboutMe') 대신 @ model.get ('aboutMe')를 완료했습니다 .. 그리고이 동기화는 매력처럼 일했습니다. 감사! –