Backbone.js - 将 json 数据放入视图中
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 模板中。我看到 ajax 请求被发送并且返回的数据是正确的。我在调用视图时将静态数据放在同一位置,并且效果很好。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是因为您在错误的时间执行模板。它应该在调用
render
时完成,如下所示:I think it's because you are executing your template at the wrong time. It should be done when
render
is called, like this: