我是 Backbone.js 和 Parse 的新手,如果我不够具体,我深表歉意。
我有一个在初始化时抛出 TypeError 的视图,即使我通过删除有问题的行来修复,也只会在代码中抛出另一个完全可以接受的错误。
这是我得到的错误:
Uncaught TypeError: Cannot call method 'bind' of undefined views.js:59
Uncaught TypeError: Cannot call method 'bind' of undefined views.js:59
这是视图的 init 函数,第 59 和 60 行:
initialize: function() {
this.model.bind("reset", this.render, this);
this.model.bind("add", this.appendNewJob, this);
}
如果您需要更多代码,请在评论中告诉我。更新:根据要求,我的 render 和 appendNewJob 函数:
render: function(eventName) {
_.each(this.model.models, function(job){
this.appendNewJob(job);
}, this);
this.delegateEvents();
return this.el;
},
appendNewJob: function(job){
this.$el.append(new JobListItemView({
model: job
}).render());
}
我的路由器:
var AppRouter = Parse.Router.extend({
initialize: function() {
$('#header').html(new HeaderView().render());
},
routes: {
"": "list",
"jobs": "list",
"settings": "viewSettings"
},
list: function() {
var self = this;
FB.getLoginStatus(function(response){
if(response.status === 'connected') {
// logged in.
self.before(function() {
self.showView('#content', new JobListView());
});
} else if(response.status === 'not_authorized') {
// not authorized.
} else {
// not connected.
$('#app').hide();
self.before(function() {
self.showView('#login', new StartView());
});
}
});
},
viewSettings: function() {
var self = this;
FB.getLoginStatus(function(response){
if(response.status === 'connected') {
// logged in.
self.before(function() {
self.showView('#content', new SettingsView());
});
} else if(response.status === 'not_authorized') {
// not authorized.
} else {
// not connected.
$('#app').hide();
self.before(function() {
self.showView('#login', new StartView());
});
}
});
$('#filterBar').hide();
},
showView: function(selector, view) {
if(this.currentView) this.currentView.close();
$(selector).html(view.render());
this.currentView = view;
return view;
},
before: function(callback) {
if(this.jobList) {
if(callback) callback.call(this);
} else {
this.jobList = new JobCollection();
var self= this;
this.jobList.fetch({
success: function() {
var joblist = new JobListView({
model: self.jobList
}).render();
$('#content').html(joblist);
if(callback) callback.call(self);
}
});
}
}
});
更新:我还使用 parse js 库代替骨干。