我对 JavaScript 和 Backbone 还很陌生,我遇到了这个错误。
Router = Backbone.Router.extend({
routes: {
":albumID": "load"
},
load: function (albumID) {
if (controller.collectionInitialized == true) {
console.log("RESET");
album.trigger("clear");
}
var album = new Album([], {
title: albumID
});
controller.trigger("collectionInit");
controller.trigger("switchAlbum", albumID);
}
});
Controller = Backbone.Model.extend({
currentAlbum: "",
collectionInitialized: false,
initialize: function () {
console.log("controller is initialized");
this.on("switchAlbum", function (newAlbum) {
this.currentAlbum = newAlbum;
});
this.on("collectionInit", function () {
this.collectionInitialized = true;
});
}
});
Album = Backbone.Collection.extend({
initialize: function (models, options) {
this.on("clear", this.clear);
},
clear: function () {
this.reset();
this.off();
}
});
我得到这个错误:Unable to get property 'trigger' of undefined or null reference
。该if
语句确保album
在触发之前已经存在clear
。以前我尝试直接调用album.reset()
但得到了同样的错误。我的猜测是这是某种范围界定问题,有人可以指出我正确的方向吗?