我目前正在通过截屏教程学习backbone.js,在此过程中,我的代码似乎停止工作,Cannot call method 'bind' of undefined
在Chrome的javascript控制台中抛出错误。错误的行包含在initialize
函数中:
window.PlaylistView = Backbone.View.extend({
tag: 'section',
className: 'playlist',
initialize: function() {
_.bindAll(this, 'render');
this.template = _.template($('#playlist-template').html());
this.collection.bind('reset', this.render); //<<<<<< THIS LINE
this.player = this.options.player;
this.library = this.options.library;
},
render: function() {
$(this.el).html(this.template(this.player.toJSON()));
this.$('button.play').toggle(this.player.isStopped());
this.$('button.pause').toggle(this.player.isPlaying());
return this;
}
});
我不知道是什么this.collection
意思,为什么视图有一个集合,而不是模型的集合?this.collection.bind()
在其他视图中使用似乎没有引发任何错误。在window.LibraryAlbumView
which callsthis.collection.trigger('select', this.model);
和 extendswindow.AlbumView
中,我没有看到在任何地方定义的任何集合window.AlbumView
,但没有引发错误。这似乎让我感到困惑。
编辑:
该错误已得到修复,因为而不是
window.Player = Backbone.Model.extend({
defaults: {
'currentAlbumIndex': 0,
'currentTrackIndex': 0,
'state': 'stop'
},
initialize: function() {
this.playlist = new Playlist();
},
我有
window.Player = Backbone.Model.extend({
defaults: {
'currentAlbumIndex': 0,
'currentTrackIndex': 0,
'state': 'stop'
},
initialize: function() {
playlist = new Playlist(); // <<< this line changed!
},
之前也this.collection
提到了这里的集合,
window.BackboneTunes = Backbone.Router.extend({
routes: {
'': 'home',
'blank': 'blank'
},
initialize: function() {
this.playlistView = new PlaylistView({
collection: window.player.playlist, // <<<< THIS ONE!
player: window.player,
library: window.library
});
this.libraryView = new LibraryView({
collection: window.library
});
},