私は JavaScript とバックボーンにかなり慣れていないので、このエラーに遭遇しました。
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()
以前は直接呼び出してみましたが、同じエラーが発生しました。私の推測では、それはある種のスコーピングの問題だと思います。誰かが私を正しい方向に向けてもらえますか?