Backbone.Model にある種のネストされたコレクションを実装しようとしています。
これを行うには、サーバーの応答を解析して配列をコレクションにラップするアダプター関数と、ヘルパー メソッドを使用せずにオブジェクト全体をシリアル化する関数を上書きする必要があります。私は2番目のものに問題があります。
var Model = Backbone.Model.extend({
urlRoot: "/model",
idAttribute: "_id",
// this wraps the arrays in the server response into a backbone collection
parse: function(resp, xhr) {
$.each(resp, function(key, value) {
if (_.isArray(value)) {
resp[key] = new Backbone.Collection(value);
}
});
return resp;
},
// serializes the data without any helper methods
toJSON: function() {
// clone all attributes
var attributes = _.clone(this.attributes);
// go through each attribute
$.each(attributes, function(key, value) {
// check if we have some nested object with a toJSON method
if (_.has(value, 'toJSON')) {
// execute toJSON and overwrite the value in attributes
attributes[key] = value.toJSON();
}
});
return attributes;
}
});
問題は toJSON の 2 番目の部分にあります。何らかの理由で
_.has(value, 'toJSON') !== true
true を返さない
誰かが何がうまくいかないのか教えてもらえますか?