lib/assets/javascripts/backbone_extensions/associations.js in backbone_extensions-0.0.23 vs lib/assets/javascripts/backbone_extensions/associations.js in backbone_extensions-0.0.24

- old
+ new

@@ -1,23 +1,24 @@ //= require backbone_extensions/include //= require underscore.string -(function(_, Backbone) { +(function() { 'use strict'; - var global = this, fn = {}; + var exports = this, _ = exports._, Backbone = exports.Backbone; + var fn = {}; function mixin(namespace, globalOptions) { - namespace = namespace || global || {}; + namespace = namespace || exports || {}; globalOptions = globalOptions || {}; - _(fn).extend({ + _.extend(fn, { mergeOptions: function() { - return _(arguments).chain().toArray().reduce(function(result, options) { return _(result).extend(options); }, {}) + return _.chain(arguments).toArray().reduce(function(result, options) { return _.extend(result, options); }, {}) .omit('class', 'className', 'inverseOf', 'parseName', 'through').value(); }, buildAssociation: function(namespace, associationType, associationName, options) { function through() { function association() { - var t = (_(options.through).isFunction() && options.through.call(this)) || _.str.camelize(options.through); + var t = (_.isFunction(options.through) && options.through.call(this)) || _.str.camelize(options.through); return this[t] && this[t]() && this[t]()[associationName] && this[t]()[associationName](); } return options.through && association.call(this); } @@ -32,13 +33,13 @@ if (options.inverseOf) { newOptions[_.str.camelize(options.inverseOf)] = function() { return self; }; } - return _((options['class'] && new options['class'](null, newOptions)) || + return _.tap((options['class'] && new options['class'](null, newOptions)) || (className && namespace[className] && new namespace[className](null, newOptions)) || - (namespace[collectionName] && new namespace[collectionName](null, newOptions))).tap(function(association) { + (namespace[collectionName] && new namespace[collectionName](null, newOptions)), function(association) { through.call(self, association); }); } var associations = { @@ -47,32 +48,32 @@ belongsTo: function() { return throughCollection.call(this) || through.call(this); } }; this.prototype[associationName] = function() { return (this._associations || (this._associations = {})) && this._associations[associationName] || - (this._associations[associationName] = (this._options && _(this._options).result(associationName)) || associations[associationType].call(this)); + (this._associations[associationName] = (this._options && _.result(this._options, associationName)) || associations[associationType].call(this)); }; }, parseAssociation: function(associationType, associationName, options) { function parseResponseWith(key, response) { var result; - return _([_.str.camelize, _.str.underscored]).any(function(fn) { + return _.any([_.str.camelize, _.str.underscored], function(fn) { var k = fn(key); return (result = response[k] && {key: k, response: response[k]}); }) && result || {response: null}; } function through(response) { - var t = parseResponseWith(_(options).result('through'), response).response, - singularAssociationName = _.singularize && _(associationName).singularize(), + var t = parseResponseWith(_.result(options, 'through'), response).response, + singularAssociationName = _.singularize && _.singularize(associationName), p = options.parseName || singularAssociationName; - return {response: t && p && _(t)[associationType === 'hasOne' ? 'result' : 'pluck'](p)}; + return {response: t && p && _[associationType === 'hasOne' && 'result' || 'pluck'](t, p)}; } if (options.parse) { - if (!_(this).has('_parsers')) { + if (!_.has(this, '_parsers')) { this._parsers = []; } var associations = { hasMany: function(assocResponse, association, newOptions) { @@ -83,18 +84,17 @@ } }; if (associations[associationType]) { var parsers = this._parsers; - if (_(parsers).isEmpty()) { - this.prototype.parse = _(this.prototype.parse).wrap(function(oldParse, response) { + if (_.isEmpty(parsers)) { + this.prototype.parse = _.wrap(this.prototype.parse, function(oldParse, response) { var self = this; - return _(oldParse.call(self, response)).tap(function(parsedResponse) { - _(parsers) - .chain() + return _.tap(oldParse.call(self, response), function(parsedResponse) { + _.chain(parsers) .map(function(parser) { - return _(parser.parseFn.call(self).call(self, parsedResponse)).tap(function(result) { + return _.tap(parser.parseFn.call(self).call(self, parsedResponse), function(result) { parser.associationFn.call(self, result.response); }).key; }) .each(function(key) { return key && delete parsedResponse[key]; @@ -103,11 +103,11 @@ }); } this._parsers.push({ parseFn: function() { - return _(options.parse).isFunction() && + return _.isFunction(options.parse) && 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)) || @@ -124,51 +124,51 @@ } }); return { included: function(source) { - var associations = _({ + var associations = _.reduce({ belongsTo: {}, hasMany: {parse: true}, hasOne: {parse: true} - }).reduce(function(associations, defaultOptions, associationType) { + }, function(associations, defaultOptions, associationType) { associations[associationType] = function(name, options) { var associationName = _.str.camelize(name); - options = _({}).extend(defaultOptions, globalOptions, options); + options = _.extend({}, defaultOptions, globalOptions, options); fn.buildAssociation.call(this, namespace, associationType, associationName, options); fn.parseAssociation.call(this, associationType, associationName, options); return this; }; return associations; }, {}); - _(source).extend(associations, { + _.extend(source, associations, { associations: function() { var self = this; - _(arguments).chain().toArray().compact().each(function(options) { - _(associations).chain().keys().each(function(associationType) { + _.chain(arguments).toArray().compact().each(function(options) { + _.chain(associations).keys().each(function(associationType) { if (options[associationType]) { - associations[associationType].call(self, options[associationType], _(options).omit(associationType)); + associations[associationType].call(self, options[associationType], _.omit(options, associationType)); } }); }); }, - extend: _(source.extend).wrap(function(oldExtend, protoProps, classProps) { - return _(oldExtend.call(this, protoProps, classProps)).tap(function(Klass) { + extend: _.wrap(source.extend, function(oldExtend, protoProps, classProps) { + return _.tap(oldExtend.call(this, protoProps, classProps), function(Klass) { var args = (protoProps || {}).associations; if (args) { - Klass.associations.apply(Klass, _([args]).flatten()); + Klass.associations.apply(Klass, _.flatten([args])); } }); }) }); - source.prototype.initialize = _(source.prototype.initialize).wrap(function(oldInitialize, attrsOrModels, options) { - this._options = this._options || _(options).clone(); + source.prototype.initialize = _.wrap(source.prototype.initialize, function(oldInitialize, attrsOrModels, options) { + this._options = this._options || _.clone(options); oldInitialize.call(this, attrsOrModels, options); }); } }; } mixin.fn = fn; - Backbone.extensions = _(Backbone.extensions || {}).extend({associations: mixin}); -}).call(this, _, Backbone); + Backbone.extensions = _.extend(Backbone.extensions || {}, {associations: mixin}); +}).call(this);