lib/assets/javascripts/backbone_extensions/associations.js in backbone_extensions-0.0.15 vs lib/assets/javascripts/backbone_extensions/associations.js in backbone_extensions-0.0.16
- old
+ new
@@ -76,28 +76,19 @@
association.clear({silent: true}).set(assocResponse, newOptions);
}
};
if (associations[associationType]) {
- var parseFunc = _(options.parse).isFunction() &&
- function(response) { return {response: options.parse.call(this, response) }; } ||
- function(response) {
- return (options.through && through.call(this, response)) ||
- (options.parseName && parseResponseWith(options.parseName, response)) ||
- (options.className && parseResponseWith(options.className, response)) ||
- parseResponseWith(associationName, response);
- };
-
- if (!this._parsers) {
- var parsers = this._parsers = [];
+ var parsers = this._parsers;
+ if (_(parsers).isEmpty()) {
this.prototype.parse = _(this.prototype.parse).wrap(function(oldParse, response) {
var self = this;
return _(oldParse.call(self, response)).tap(function(parsedResponse) {
_(parsers)
.chain()
.map(function(parser) {
- return _(parser.parseFn.call(self, parsedResponse)).tap(function(result) {
+ return _(parser.parseFn.call(self).call(self, parsedResponse)).tap(function(result) {
parser.associationFn.call(self, result.response);
}).key;
})
.each(function(key) {
return key && delete parsedResponse[key];
@@ -105,11 +96,20 @@
});
});
}
this._parsers.push({
- parseFn: parseFunc,
+ parseFn: function() {
+ return _(options.parse).isFunction() &&
+ function(response) { return {response: options.parse.call(this, response) }; } ||
+ function(response) {
+ return (options.through && through.call(this, response)) ||
+ (options.parseName && parseResponseWith(options.parseName, response)) ||
+ (options.className && parseResponseWith(options.className, response)) ||
+ parseResponseWith(associationName, response);
+ };
+ },
associationFn: function(assocResponse) {
associations[associationType].call(this, assocResponse, this[associationName](), mergeAssociationOptions(options, this._options));
}
});
}
@@ -122,9 +122,12 @@
belongsTo: {}, hasMany: {parse: true}, hasOne: {parse: true}
}).reduce(function(associations, defaultOptions, associationType) {
associations[associationType] = function(name, options) {
var associationName = _.str.camelize(name);
options = _({}).extend(defaultOptions, options);
+ if (options.parse && !_(this).has('_parsers')) {
+ this._parsers = [];
+ }
buildAssociation.call(this, associationType, associationName, options);
parseAssociation.call(this, associationType, associationName, options);
return this;
};
return associations;