不知怎的,当我初始化我的看法,我得到一个错误Uncaught TypeError: Cannot read property 'toJSON' of undefined
Backbonejs - 我如何初始化视图?
我有以下代码:
//Model
var Song = Backbone.Model.extend({
defaults: {
title: 'Red',
artist: 'Taylor Swift',
filename: 'audio1.mp3',
playlist: 0
},
});
//Collection
var Playlist = Backbone.Collection.extend({
model: Song,
localStorage: new Backbone.LocalStorage('playlist'),
initialize: function(models,options) {
_.extend(this,_.pick(options, 'currentTrack'));
this.fetch({ajaxSync: true})
},
url: 'metadata',
parse: function(response){
return response
},
currentTrack: 0,
currentTime: 0
});
//View
var Player = Backbone.View.extend({
tagName: "audio",
id: "player",
model: playlist,
currentSong: function(){
console.log(this.model);
return this.model.at(this.model.currentTrack).toJSON();
},
events: {
'ended': 'next',
'pause': 'saveTime'
},
newTemplate: _.template('<source src="<%= filename %>" >'),
initialize: function(){
this.render();
},
render: function(){
this.$el.html(this.newTemplate(this.currentSong()));
$(document.body).html(this.el);
},
next: function(){
if(this.model.length == this.model.currentTrack){
console.log('End of the playlist');
}
else{
this.model.currentTrack++;
console.log('currentTrack: ', this.model.currentTrack);
this.render();
this.el.src = this.currentSong().filename;
this.el.play();
}
},
saveTime: function(){
console.log('Saving currentTime:', this.el.currentTime);
this.model.currentTime = this.el.currentTime;
}
});
var playlist = new Playlist();
var player = new Player({model: playlist});
不知怎的,当我初始化player
查看playlist
colleciton尚未填充呢。我可以从控制台运行var player = new Player({model: playlist})
。
您很可能需要监听收集事件并渲染视图。如果你调试,什么变量是未定义的?另外,'playlist'是一个集合,所以它应该是'collection:playlist' – vassiliskrikonis
这是this.model返回undefined – medicengonzo
'model:playlist,'在视图属性中(在'extend'中)没用。这是一个应该在实例化时传递的选项,就像你在底部一样。 –