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();
}
});
for some reason I can't get the model data to be injects into the template for HomeView. I see the ajax request being sent off and the data coming back is correct. I put static data in the same place when calling the view and it worked fine.
Any suggestions?
I think it's because you are executing your template at the wrong time. It should be done when render
is called, like this:
var HomeView = Backbone.View.extend({
el: '#container',
template: _.template($("#home-template").html()),
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.