2011-11-23 4 views
1
User = Backbone.Model.extend({ 
    urlRoot: "users", 

    initialize: function(){ 
    this.fetch(); 
    } 
}); 

    var HomeView = Backbone.View.extend({ 
    el: '#container', 
    template: _.template($("#home-template").html(), this.model), 

    render: function() { 
     $(this.el).html(this.template); 
     return this; 
    } 
    }); 

    var App = Backbone.Router.extend({ 
     routes: { 

      '/home':  'home', 
     }, 

     home: function() { 
     var user = new User({id: 1}); 
     homeView = new HomeView({ 
      model: user 
     }); 
     this.homeView.render(); 
     } 
    }); 

어떤 이유로 든 모델 데이터를 HomeView 템플릿에 삽입 할 수 없습니다. 아약스 요청을 보냈고 다시 오는 데이터가 맞습니다. 뷰를 호출 할 때 정적 데이터를 같은 위치에 놓았고 정상적으로 작동했습니다.Backbone.js - json 데이터를보기로 가져옴

제안 사항?

답변

3

틀린 시간에 템플릿을 실행하고 있기 때문에 생각합니다. 다음과 같이 render을 호출하면 완료되어야합니다.

var HomeView = Backbone.View.extend({ 
    el: '#container', 
    template: _.template($("#home-template").html()), 

    render: function() { 
    $(this.el).html(this.template(this.model.toJSON())); 
    return this; 
    } 
}); 
+0

정확히 그랬습니다. 감사! – user577808