我正在尝试使用 peepcode 做一个教程主干项目,但我被卡住了。
我正在尝试通过从集合中创建新视图来从控制台呈现视图。这是我的代码
(function($) {
window.Album = Backbone.Model.extend({
isFirstTrack: function(index) {
return index == 0;
},
isLastTrack: function(index) {
return index >= this.get('tracks').length - 1;
},
trackUrlAtIndex: function(index) {
if (this.get('tracks').length >= index) {
return this.get('tracks')[index].url;
}
return null;
}
});
window.Albums = Backbone.Collection.extend({
model: Album,
url: '/albums'
});
window.Album = Backbone.Model.extend({});
window.AlbumView = Backbone.View.extend({
tagName: 'li',
className: 'album',
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.template = _.template($('#album-template').html());
},
render: function() {
var renderedContent = this.template(this.model.toJSON());
$(this.el).html(renderedContent);
return this;
}
});
window.LibraryAlbumView = AlbumView.extend({
});
window.LibraryView = Backbone.View.extend({
tagName: 'section',
className: 'library',
initialize: function () {
_.bindAll(this, 'render');
this.template = _.template($('#library-template').html());
this.collection.bind('reset', this.render);
},
render: function() {
var $albums,
collection = this.collection;
$(this.el).html(this.template({}));
$albums = this.$(".albums");
collection.each(function(album) {
var view = new LibraryAlbumView({
model: album,
collection: collection
});
$albums.append(view.render().el);
});
return this;
}
});
})(jQuery);
当我libraryView = new LibraryView({ collection: library })
在控制台中输入时,我得到以下响应:
TypeError:无法调用 null 的“替换”方法
谁能帮我吗?