在主干视图的点击事件中创建新的主干视图
Create new Backbone Views in click event of Backbone View
在PodcastView的点击事件中,我想创建多个新的PodcastItemView对象。jquery $.get 完美地工作,顺便说一句。
如果我在启动函数中执行console.debug(this.pinfo),我会收到我的播客项目的JSON数组(title,desc,url,...),所以这不是问题。此外,它通过此数组迭代 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 项中。我收到以下错误:
TypeError: obj[implementation] 不是函数 (Backbone.js:225)
这是我的播客项目视图。
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。
希望这有帮助。
相关文章:
- 如何使用javascript从主svg对象动态创建svg视图框
- Kendo:我该如何在树视图中创建一个递归的hieiarchy
- 需要一个与HTML5<音频>.哪些没有't创建或销毁视图
- 遍历SC.ManyArrray以创建要附加到SproutCore中的另一个视图的自定义视图
- 在Django中,可以在视图中创建一个包含js的变量,然后在循环模板标记中使用它
- 调用在同一视图中使用javascript在视图中创建的函数
- 自定义筛选器不'我不使用django数据表视图创建的ajax.data.Json
- 动态创建角度视图
- 主干网的生命周期.js创建过程中的视图
- 在PHP循环中动态创建jQuery列表视图
- 创建表时在marionete.js中嵌套视图
- 在创建视图mvc.net的过程中使用javascript
- 在 Node.js 视图中创建 OpenLayers 映射
- 如何在Backbone中创建自实例化视图
- 创建从单个模板继承的多个动态视图
- 如何在WinJS中为列表视图创建排序功能
- ExtJS 视图创建
- 为两个视图创建指令
- 在我重新解析从部分视图创建的表单后,jQuery提交事件不触发
- Pouchdb,视图创建似乎使正常的数据不可用