From 40bd719a65d41fb4b7e121f63d90ecaf47391b8d Mon Sep 17 00:00:00 2001 From: Harry Brundage Date: Mon, 23 Jul 2012 16:53:04 -0400 Subject: [PATCH] Vendor batman once more --- vendor/assets/javascripts/batman/batman.js | 314 ++++++++++----------- 1 file changed, 147 insertions(+), 167 deletions(-) diff --git a/vendor/assets/javascripts/batman/batman.js b/vendor/assets/javascripts/batman/batman.js index f27cef7..c86579e 100644 --- a/vendor/assets/javascripts/batman/batman.js +++ b/vendor/assets/javascripts/batman/batman.js @@ -2262,16 +2262,16 @@ var _ref; return (_ref = document.getElementById(elementID)) != null ? typeof _ref.scrollIntoView === "function" ? _ref.scrollIntoView() : void 0 : void 0; }, - querySelectorAll: window.jQuery != null ? function(node, selector) { + querySelectorAll: (typeof window !== "undefined" && window !== null ? window.jQuery : void 0) != null ? function(node, selector) { return jQuery(selector, node); - } : document.querySelectorAll != null ? function(node, selector) { + } : (typeof document !== "undefined" && document !== null ? document.querySelectorAll : void 0) != null ? function(node, selector) { return node.querySelectorAll(selector); } : function() { return Batman.developer.error("Please include either jQuery or a querySelectorAll polyfill, or set Batman.DOM.querySelectorAll to return an empty array."); }, - querySelector: window.jQuery != null ? function(node, selector) { + querySelector: (typeof window !== "undefined" && window !== null ? window.jQuery : void 0) != null ? function(node, selector) { return jQuery(selector, node)[0]; - } : document.querySelector != null ? function(node, selector) { + } : (typeof document !== "undefined" && document !== null ? document.querySelector : void 0) != null ? function(node, selector) { return node.querySelector(selector); } : function() { return Batman.developer.error("Please include either jQuery or a querySelector polyfill, or set Batman.DOM.querySelector to an empty function."); @@ -8375,7 +8375,7 @@ this.foreignKeyValue = foreignKeyValue; this.association = association; base = new Batman.Set; - AssociationSet.__super__.constructor.call(this, base, 'hashKey'); + AssociationSet.__super__.constructor.call(this, base, '_batmanID'); } AssociationSet.prototype.loaded = false; @@ -8585,7 +8585,7 @@ PolymorphicAssociationSetIndex.prototype._resultSetForKey = function(key) { var _this = this; return this._storage.getOrSet(key, function() { - return new Batman.PolymorphicAssociationSet(key, _this.type, _this.association); + return new _this.association.proxyClass(key, _this.type, _this.association); }); }; @@ -8611,7 +8611,7 @@ AssociationSetIndex.prototype._resultSetForKey = function(key) { var _this = this; return this._storage.getOrSet(key, function() { - return new Batman.AssociationSet(key, _this.association); + return new _this.association.proxyClass(key, _this.association); }); }; @@ -8952,7 +8952,7 @@ return acc.concat(pairForList("" + key + "[]", element)); }, []); default: - return [[key, object]]; + return [[key, object != null ? object : ""]]; } })(); }; @@ -9863,11 +9863,20 @@ name: Batman.helpers.camelize(Batman.helpers.singularize(this.label)) }; this.options = Batman.extend(defaultOptions, this.defaultOptions, options); - encoder = this.encoder(); - if (!this.options.saveInline) { - encoder.encode = false; + if (this.options.nestUrl) { + if (!(this.model.urlNestsUnder != null)) { + developer.error("You must persist the the model " + this.model.constructor.name + " to use the url helpers on an association"); + } + this.model.urlNestsUnder(Batman.helpers.underscore(this.getRelatedModel().get('resourceName'))); + } + if (this.options.extend != null) { + Batman.extend(this, this.options.extend); } - this.model.encode(label, encoder); + encoder = { + encode: this.options.saveInline ? this.encoder() : false, + decode: this.decoder() + }; + this.model.encode(this.label, encoder); self = this; getAccessor = function() { return self.getAccessor.call(this, self, this.model, this.label); @@ -9877,12 +9886,6 @@ set: model.defaultAccessor.set, unset: model.defaultAccessor.unset }); - if (this.options.nestUrl) { - if (!(this.model.urlNestsUnder != null)) { - developer.error("You must persist the the model " + this.model.constructor.name + " to use the url helpers on an association"); - } - this.model.urlNestsUnder(Batman.helpers.underscore(this.getRelatedModel().get('resourceName'))); - } } Association.prototype.getRelatedModel = function() { @@ -9906,14 +9909,6 @@ return record.get('attributes').set(this.label, value); }; - Association.prototype.encoder = function() { - return Batman.developer.error("You must override encoder in Batman.Association subclasses."); - }; - - Association.prototype.setIndex = function() { - return Batman.developer.error("You must override setIndex in Batman.Association subclasses."); - }; - Association.prototype.inverse = function() { var inverse, relatedAssocs, _this = this; @@ -9954,6 +9949,8 @@ return PluralAssociation.__super__.constructor.apply(this, arguments); } + PluralAssociation.prototype.proxyClass = Batman.AssociationSet; + PluralAssociation.prototype.isSingular = false; PluralAssociation.prototype.setForRecord = Batman.Property.wrapTrackingPrevention(function(record) { @@ -9961,7 +9958,7 @@ if (id = record.get(this.primaryKey)) { return this.setIndex().get(id); } else { - return new Batman.AssociationSet(void 0, this); + return new this.proxyClass(void 0, this); } }); @@ -10044,65 +10041,65 @@ HasManyAssociation.prototype.encoder = function() { var association; association = this; - return { - encode: function(relationSet, _, __, record) { - var jsonArray; - if (!association.options.saveInline) { - return; - } - if (relationSet != null) { - jsonArray = []; - relationSet.forEach(function(relation) { - var relationJSON; - relationJSON = relation.toJSON(); - if (!association.inverse() || association.inverse().options.encodeForeignKey) { - relationJSON[association.foreignKey] = record.get(association.primaryKey); - } - return jsonArray.push(relationJSON); + return function(relationSet, _, __, record) { + var jsonArray; + if (relationSet != null) { + jsonArray = []; + relationSet.forEach(function(relation) { + var relationJSON; + relationJSON = relation.toJSON(); + if (!association.inverse() || association.inverse().options.encodeForeignKey) { + relationJSON[association.foreignKey] = record.get(association.primaryKey); + } + return jsonArray.push(relationJSON); + }); + } + return jsonArray; + }; + }; + + HasManyAssociation.prototype.decoder = function() { + var association; + association = this; + return function(data, key, _, __, parentRecord) { + var existingRecord, existingRelations, jsonObject, newRelations, record, relatedModel, savedRecord, _i, _len; + if (relatedModel = association.getRelatedModel()) { + existingRelations = association.getFromAttributes(parentRecord) || association.setForRecord(parentRecord); + newRelations = existingRelations.filter(function(relation) { + return relation.isNew(); + }).toArray(); + for (_i = 0, _len = data.length; _i < _len; _i++) { + jsonObject = data[_i]; + record = new relatedModel(); + record._withoutDirtyTracking(function() { + return this.fromJSON(jsonObject); }); - } - return jsonArray; - }, - decode: function(data, key, _, __, parentRecord) { - var existingRecord, existingRelations, jsonObject, newRelations, record, relatedModel, savedRecord, _i, _len; - if (relatedModel = association.getRelatedModel()) { - existingRelations = association.getFromAttributes(parentRecord) || association.setForRecord(parentRecord); - newRelations = existingRelations.filter(function(relation) { - return relation.isNew(); - }).toArray(); - for (_i = 0, _len = data.length; _i < _len; _i++) { - jsonObject = data[_i]; - record = new relatedModel(); - record._withoutDirtyTracking(function() { + existingRecord = relatedModel.get('loaded').indexedByUnique('id').get(record.get('id')); + if (existingRecord != null) { + existingRecord._withoutDirtyTracking(function() { return this.fromJSON(jsonObject); }); - existingRecord = relatedModel.get('loaded').indexedByUnique('id').get(record.get('id')); - if (existingRecord != null) { - existingRecord._withoutDirtyTracking(function() { + record = existingRecord; + } else { + if (newRelations.length > 0) { + savedRecord = newRelations.shift(); + savedRecord._withoutDirtyTracking(function() { return this.fromJSON(jsonObject); }); - record = existingRecord; - } else { - if (newRelations.length > 0) { - savedRecord = newRelations.shift(); - savedRecord._withoutDirtyTracking(function() { - return this.fromJSON(jsonObject); - }); - record = savedRecord; - } - } - record = relatedModel._mapIdentity(record); - existingRelations.add(record); - if (association.options.inverseOf) { - record.set(association.options.inverseOf, parentRecord); + record = savedRecord; } } - existingRelations.markAsLoaded(); - } else { - Batman.developer.error("Can't decode model " + association.options.name + " because it hasn't been loaded yet!"); + record = relatedModel._mapIdentity(record); + existingRelations.add(record); + if (association.options.inverseOf) { + record.set(association.options.inverseOf, parentRecord); + } } - return existingRelations; + existingRelations.markAsLoaded(); + } else { + Batman.developer.error("Can't decode model " + association.options.name + " because it hasn't been loaded yet!"); } + return existingRelations; }; }; @@ -10120,6 +10117,8 @@ __extends(PolymorphicHasManyAssociation, _super); + PolymorphicHasManyAssociation.prototype.proxyClass = Batman.PolymorphicAssociationSet; + PolymorphicHasManyAssociation.prototype.isPolymorphic = true; function PolymorphicHasManyAssociation(model, label, options) { @@ -10161,18 +10160,10 @@ }; PolymorphicHasManyAssociation.prototype.encoder = function() { - var association, encoder; + var association; association = this; - encoder = PolymorphicHasManyAssociation.__super__.encoder.apply(this, arguments); - encoder.encode = function(relationSet, _, __, record) { + return function(relationSet, _, __, record) { var jsonArray; - if (association._beingEncoded) { - return; - } - association._beingEncoded = true; - if (!association.options.saveInline) { - return; - } if (relationSet != null) { jsonArray = []; relationSet.forEach(function(relation) { @@ -10183,10 +10174,8 @@ return jsonArray.push(relationJSON); }); } - delete association._beingEncoded; return jsonArray; }; - return encoder; }; return PolymorphicHasManyAssociation; @@ -10266,30 +10255,33 @@ HasOneAssociation.prototype.encoder = function() { var association; association = this; - return { - encode: function(val, key, object, record) { - var json; - if (!association.options.saveInline) { - return; - } - if (json = val.toJSON()) { - json[association.foreignKey] = record.get(association.primaryKey); - } - return json; - }, - decode: function(data, _, __, ___, parentRecord) { - var record, relatedModel; - relatedModel = association.getRelatedModel(); - record = new relatedModel(); - record._withoutDirtyTracking(function() { - return this.fromJSON(data); - }); - if (association.options.inverseOf) { - record.set(association.options.inverseOf, parentRecord); - } - record = relatedModel._mapIdentity(record); - return record; + return function(val, key, object, record) { + var json; + if (!association.options.saveInline) { + return; } + if (json = val.toJSON()) { + json[association.foreignKey] = record.get(association.primaryKey); + } + return json; + }; + }; + + HasOneAssociation.prototype.decoder = function() { + var association; + association = this; + return function(data, _, __, ___, parentRecord) { + var record, relatedModel; + relatedModel = association.getRelatedModel(); + record = new relatedModel(); + record._withoutDirtyTracking(function() { + return this.fromJSON(data); + }); + if (association.options.inverseOf) { + record.set(association.options.inverseOf, parentRecord); + } + record = relatedModel._mapIdentity(record); + return record; }; }; @@ -10337,37 +10329,34 @@ } BelongsToAssociation.prototype.encoder = function() { - var association, encoder; + return function(val) { + return val.toJSON(); + }; + }; + + BelongsToAssociation.prototype.decoder = function() { + var association; association = this; - encoder = { - encode: false, - decode: function(data, _, __, ___, childRecord) { - var inverse, record, relatedModel; - relatedModel = association.getRelatedModel(); - record = new relatedModel(); - record._withoutDirtyTracking(function() { - return this.fromJSON(data); - }); - record = relatedModel._mapIdentity(record); - if (association.options.inverseOf) { - if (inverse = association.inverse()) { - if (inverse instanceof Batman.HasManyAssociation) { - childRecord.set(association.foreignKey, record.get(association.primaryKey)); - } else { - record.set(inverse.label, childRecord); - } + return function(data, _, __, ___, childRecord) { + var inverse, record, relatedModel; + relatedModel = association.getRelatedModel(); + record = new relatedModel(); + record._withoutDirtyTracking(function() { + return this.fromJSON(data); + }); + record = relatedModel._mapIdentity(record); + if (association.options.inverseOf) { + if (inverse = association.inverse()) { + if (inverse instanceof Batman.HasManyAssociation) { + childRecord.set(association.foreignKey, record.get(association.primaryKey)); + } else { + record.set(inverse.label, childRecord); } } - childRecord.set(association.label, record); - return record; } + childRecord.set(association.label, record); + return record; }; - if (this.options.saveInline) { - encoder.encode = function(val) { - return val.toJSON(); - }; - } - return encoder; }; BelongsToAssociation.prototype.apply = function(base) { @@ -10497,40 +10486,31 @@ } }; - PolymorphicBelongsToAssociation.prototype.encoder = function() { - var association, encoder; + PolymorphicBelongsToAssociation.prototype.decoder = function() { + var association; association = this; - encoder = { - encode: false, - decode: function(data, key, response, ___, childRecord) { - var foreignTypeValue, inverse, record, relatedModel; - foreignTypeValue = response[association.foreignTypeKey] || childRecord.get(association.foreignTypeKey); - relatedModel = association.getRelatedModelForType(foreignTypeValue); - record = new relatedModel(); - record._withoutDirtyTracking(function() { - return this.fromJSON(data); - }); - record = relatedModel._mapIdentity(record); - if (association.options.inverseOf) { - if (inverse = association.inverseForType(foreignTypeValue)) { - if (inverse instanceof Batman.PolymorphicHasManyAssociation) { - childRecord.set(association.foreignKey, record.get(association.primaryKey)); - childRecord.set(association.foreignTypeKey, foreignTypeValue); - } else { - record.set(inverse.label, childRecord); - } + return function(data, key, response, ___, childRecord) { + var foreignTypeValue, inverse, record, relatedModel; + foreignTypeValue = response[association.foreignTypeKey] || childRecord.get(association.foreignTypeKey); + relatedModel = association.getRelatedModelForType(foreignTypeValue); + record = new relatedModel(); + record._withoutDirtyTracking(function() { + return this.fromJSON(data); + }); + record = relatedModel._mapIdentity(record); + if (association.options.inverseOf) { + if (inverse = association.inverseForType(foreignTypeValue)) { + if (inverse instanceof Batman.PolymorphicHasManyAssociation) { + childRecord.set(association.foreignKey, record.get(association.primaryKey)); + childRecord.set(association.foreignTypeKey, foreignTypeValue); + } else { + record.set(inverse.label, childRecord); } } - childRecord.set(association.label, record); - return record; } + childRecord.set(association.label, record); + return record; }; - if (this.options.saveInline) { - encoder.encode = function(val) { - return val.toJSON(); - }; - } - return encoder; }; return PolymorphicBelongsToAssociation;