传递参数的骨干js
Backbone js passing arguments
我刚开始阅读Backbone js,在Backbone js中传递参数时遇到了一些严重问题。
var Song = Backbone.Model.extend();
var Songs = Backbone.Collection.extend({
model: Song
});
var SongView = Backbone.View.extend({
el: "li",
render: function() {
this.$el.html(this.model.get("title"));
return this;
}
});
var SongsView = Backbone.View.extend({
el: "ul",
initialize: function() {
this.model.on("add", this.onSongAdded, this);
},
onSongAdded: function(song) { // when object is added to a collection add event is triggerd
// the handler for this event get an argument which is the object that was just added
//in this case it refers to a song model so we simply pass it to our songView which is responsible for rendering a song an then we use jquery append method
// to append it to our list
var songView = new SongView({
model: Song
});
this.$el.append(songView.render().$el);
},
render: function() {
var self = this;
this.model.each(function(song) { //
var songView = new SongView({
model: Song
});
self.$el.append(songView.render().$el);
});
}
});
var songs = new Songs([
new Song({
title: "1"
}),
new Song({
title: "2"
}),
new Song({
title: "3"
})
]);
var song_1 = new Song({
title: "hello"
});
var songsView = new SongsView({
el: "#songs",
model: Songs
});
songsView.render();
正如你所看到的,我有这个功能:onSongAdded我们有一些内置的事件,如add,它可以获得3个参数,如下所示:添加(集合、模型、选项)如何在代码中使用这些参数?你能帮我吗?
el
选项用于将视图指向DOM中已存在的元素。项目视图应该创建新的<li>
元素,因此应该使用tagName
选项。
在集合视图构造函数中,您定义了el
选项,并且在实例化它时传递了一个不同的el
选项。如果#songs
是DOM中的<uL>
,那么在构造函数中定义el: "ul",
就没有用了。
此外,不需要手动实例化模型,只需将对象传递到集合中,集合就会在内部完成。不要将集合作为model
传递,而是将其作为collection
传递。
相关文章:
- 要求未定义JS回调参数
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 从node.js向CasperJS传递参数数组
- 在JS参数中放置引号
- Web Api 2属性路由和Angular JS参数
- 列表.js参数问题
- 在 HTML 中调用 JS 参数
- 为什么 %26 在作为 js 参数传递时被解码
- 从 HTML 传递 JS-参数
- 这是有效的 js 参数吗?
- 使用弹片路由敲除 JS 参数
- JS参数绑定不起作用
- 角度 js 参数 'DemoCtrl' 不是
- 由js参数中的数字引起的意外TOKEN非法
- Express JS:参数未定义
- 当使用button_to时,传递JS参数给控制器
- Rails将生成的js参数从表单传递到控制器
- Node.js参数不能正常工作
- 为什么有些js参数需要引号,而另一些则不需要't
- 可以't似乎将JS参数(变量)传递给Google Map API v3函数