バックボーンコレクションを反復処理して、コレクション内のモデルから派生したオブジェクトの配列を取得する必要があります。
問題は、次のオブジェクト内にモデル定義を作成するときに、コレクションが定義されたメソッドにアクセスできるようにする方法がわからないことです。Backbone.Model.extend({})
これは私がこれまでに持っているアーキテクチャの例です:
// THE MODEL
var TheModel = Backbone.Model.extend({
// THE FOLLOWING IS NOT AVAILABLE AS A METHOD OF THE MODEL:
// This isn't the actual method, but I though it might be helpful to
// to demonstrate that kind of thing the method needs to do (manipulate:
// map data and make it available to an external library)
get_derived_object: function(){
return this.get('lat') + '_' this.get('lon');
}
});
// THE COLLECTION:
var TheCollection = Backbone.Collection.extend({
// Use the underscore library to iterate through the list of
// models in the collection and get a list the objects returned from
// the "get_derived_object()" method of each model:
get_list_of_derived_model_data: function(){
return _.map(
this.models,
function(theModel){
// This method is undefined here, but this is where I would expect
// it to be:
return theModel.get_derived_object();
}
);
}
});
ここでどこが間違っているのかわかりませんが、いくつかの推測があります。*コレクションはモデルを適切に反復していません。*パブリックメソッドはBackbone.Model.extend({})で定義できません。 *パブリックメソッドの間違った場所を探しています*Backbone.jsの使用目的の誤解から派生した他のアーキテクチャ上の間違い
どんな助けでもありがたいです、どうもありがとう!
編集
問題は、このコードに実際にバグがあったことでした。コレクションにデータが入力されたとき、モデルタイプとして「TheModel」への参照がなかったため、独自のモデルを作成しました。
コレクションを定義するときに、次のコード行を追加する必要がありました。model: TheModel
var theCollection = Backbone.Collection.extend({
model: TheModel,
...
});