私は研究プロジェクトを引き継いでおり、かなりうまくやっていますが、最近、Backbone に実装されている JS 構造を誤解していると思います。モデルまたはコレクションの構造と、オブジェクトまたは関数のどちらを返すかについて混乱しています。
プロジェクトのモデルの現在の構造は次のとおりです。
define([…], function(…) {
var measureModel = Backbone.Model.extend({
defaults: {…},
initialize: function(){…}
…
});
return measureModel; //here there is no function, so I assume its an object
});
コレクションの場合:
define([…], function(…){
var measuresCollection = Backbone.Collection.extend({
model: measureModel,
initialize: function(){…}
});
return new measuresCollection(); //here I assume it is an function
});
上記の構造で新しいモデルとコレクションを作成しましたが、以下のエラーが発生したため、これも試しました:
define([…], function(…){
return Backbone.Collection.extend({ // here I just return the Object
model: newerModel,
initialize: function(){…}
});
});
この最初の構造に従って、いくつかの新しいモデルとコレクションで、最後の return ステートメントの省略に応じて、またはオブジェクトを完全に返すだけで、エラーUncaught TypeError: object is not a function
またはUncaught TypeError: [Object object] is not a function
またはが発生します。Uncaught TypeError: undefined is not a function
次のように、別のビューでコンストラクターを呼び出しています。this.newerCollection = new NewerCollection();
- 同じ構造を使用してこれらのエラーが発生するのはなぜですか?
- 変数を作成して返すことと、そのまま返すことの違いは何ですか?
- なぜ私はある方法を別の方法で使用するのでしょうか?