Javascript 嵌套视图主干
我正在尝试创建嵌套视图。Backendview调用另一个视图Listpostview但不起作用。我在嵌套视图LiasPostView中插入了一个console.log,以查看它是否被调用但从未打印 以下为外部视图代码:Javascript 嵌套视图主干,javascript,android,jquery,backbone.js,mobile,Javascript,Android,Jquery,Backbone.js,Mobile,我正在尝试创建嵌套视图。Backendview调用另一个视图Listpostview但不起作用。我在嵌套视图LiasPostView中插入了一个console.log,以查看它是否被调用但从未打印 以下为外部视图代码: var BackendView = Backbone.View.extend({ tagName: "p", events: { "touchend": "goToDetails" }, template: Handlebars.
var BackendView = Backbone.View.extend({
tagName: "p",
events: {
"touchend": "goToDetails"
},
template: Handlebars.compile(template),
initialize: function () {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render: function (eventName) {
console.log(this.model);
var list=new ListPostView({model:this.model});
list.render();
return this;
},
});
return BackendView;
});
这是上面视图调用的ListPostView的代码:
var ListPostView = Backbone.View.extend({
tagName: "ul",
id: "list",
template: Handlebars.compile(template),
initialize: function () {
console.log(this.model);
this.model.bind("reset", this.render, this);
},
render: function (eventName) {
console.log("dddd"+this.model);<---this console.log will never called!?
$(this.el).empty();
_.each(this.model.models, function (ad) {
$(this.el).append(new SinglePostView({
model: ad
}).render().el);
}, this);
return this;
}
});
return ListPostView;
});
var SinglePostView = Backbone.View.extend({
tagName: "li",
events: {
"touchend": "goToDetails"
},
template: Handlebars.compile(template),
initialize: function () {
//console.log(ad);
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render: function (eventName) {
var ad = this.model.toJSON();
ad.cid = this.model.cid;
$(this.el).html(this.template(ad));
console.log(this.template(ad));
return this;
},
});
return SinglePostView;
});
在对
BackendView
render方法调用render之后,它看起来不像是在使用list
变量
我看到调用了
list.render()
,但没有在DOM中的任何位置插入list.$el
元素。我要测试的第一件事是从BackendView
callthis.$el.append(list.$el)
没有人能帮我吗?BackendView#render
调用在哪里?还有一些建议:(1)如果视图的模型是集合,请使用this.collection
以避免混淆。(2) 使用this.collection.each(…)
代替\uu.each(this.collection.models,…)
。(3) 使用this.$el
代替$(this.el)
。(4) 在上使用而不是绑定
@muistooshort(4)仍然是个坏主意,因为它会导致内存泄漏,请使用this.listenTo(this.model,
。@Andrew但您仍然需要调用或,listenTo
只是让它更简单而已。