[英]Create new Backbone Views in click event of Backbone View
在PodcastView的click事件中,我想创建多个新的PodcastItemView对象。 jQuery $ .get可以正常工作,顺便说一句。
如果我在启动功能中执行console.debug(this.pinfo),则会收到播客项(标题,说明,URL等)的JSON数组,因此这不是问题。 此外,它还会通过该数组迭代x次,因此也可以使用。
PodcastView = Backbone.View.extend({
tagName: "li",
itemTpl: _.template($("#podcast-item").html()),
events: {
"click .p-click" : "start"
},
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function() {
this.$el.html(this.itemTpl(this.model.toJSON()));
return this;
},
start: function() {
$.get(restUri + "podcast/" + this.model.get("title") + "/items", _.bind(function(data) {
this.pinfo = data;
_.each(this.pinfo, function(o) {
var v = new PodcastItemView({model: o});
$("#podcast-items").append(v.render().el);
}, this);
}));
}
});
但是,无效的是创建新的PodcastItemView并将其附加到#podcast-items。 我收到以下错误:
TypeError:obj [implementation]不是一个函数(Backbone.js:225)
这是我的PodcastItemView。
PodcastItemView = Backbone.View.extend({
tagName: "div",
itemTpl: _.template($("#podcast-item-list").html()),
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function() {
this.$el.html(this.itemTpl(this.model.toJSON()));
return this;
}
});
感谢您的每一个提示或回应。
将start
函数重写为:
start: function() {
$.get(restUri + "podcast/" + this.model.get("title") + "/items", _.bind(function(data) {
this.pinfo = data;
_.each(this.pinfo, function(o) {
var model = new Backbone.Model(o)
var v = new PodcastItemView({model: model});
$("#podcast-items").append(v.render().el);
}, this);
}));
如注释中所述,您的代码失败,因为您尝试将本机对象而不是Backbone.Model绑定到视图。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.