mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-24 20:26:13 +10:00
Updated for Backbone-associations - v0.5.1
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
(function(){var z=this,h,f,w,n,u,v,C,s,D,E;"undefined"===typeof window?(h=require("underscore"),f=require("backbone"),"undefined"!==typeof exports&&(exports=module.exports=f)):(h=z._,f=z.Backbone);w=f.Model;n=f.Collection;u=w.prototype;v=n.prototype;D=/[\.\[\]]+/g;C="change add remove reset sort destroy".split(" ");E=["reset","sort"];f.Associations={VERSION:"0.5.1"};f.Associations.Many=f.Many="Many";f.Associations.One=f.One="One";f.Associations.Self=f.Self="Self";s=f.AssociatedModel=f.Associations.AssociatedModel=
|
||||
w.extend({relations:void 0,_proxyCalls:void 0,get:function(a){var c=u.get.call(this,a);return c?c:this._getAttr.apply(this,arguments)},set:function(a,c,d){var b;h.isObject(a)||null==a?(b=a,d=c):(b={},b[a]=c);a=this._set(b,d);this._processPendingEvents();return a},_set:function(a,c){var d,b,F,e,l=this;if(!a)return this;for(d in a)if(b||(b={}),d.match(D)){var f=A(d);e=h.initial(f);f=f[f.length-1];e=this.get(e);e instanceof s&&(e=b[e.cid]||(b[e.cid]={model:e,data:{}}),e.data[f]=a[d])}else e=b[this.cid]||
|
||||
(b[this.cid]={model:this,data:{}}),e.data[d]=a[d];if(b)for(F in b)e=b[F],this._setAttr.call(e.model,e.data,c)||(l=!1);else l=this._setAttr.call(this,a,c);return l},_setAttr:function(a,c){var d;c||(c={});if(c.unset)for(d in a)a[d]=void 0;this.parents=this.parents||[];this.relations&&h.each(this.relations,function(b){var d=b.key,e=b.relatedModel,l=b.collectionType,p=b.map,k=this.attributes[d],q=k&&k.idAttribute,g,t,m,r,B=!1;!e||e.prototype instanceof w||(e=h.isFunction(e)?e.call(this,b,a):e);e&&h.isString(e)&&
|
||||
(e=e===f.Self?this.constructor:x(e));l&&h.isString(l)&&(l=x(l));p&&h.isString(p)&&(p=x(p));t=b.options?h.extend({},b.options,c):c;if(a[d]){g=h.result(a,d);g=p?p(g):g;if(b.type===f.Many){if(l&&!l.prototype instanceof n)throw Error("collectionType must inherit from Backbone.Collection");g instanceof n?(m=g,B=k!==g):(k?(m=k,m._deferEvents=!0):m=l?new l:this._createCollection(e),m[t.reset?"reset":"set"](g,t))}else b.type===f.One&&e&&(g instanceof s?(m=g,B=k!==g):k?k&&g[q]&&k.get(q)===g[q]?(k._deferEvents=
|
||||
!0,k._set(g,t),m=k):m=new e(g,t):m=new e(g,t));r=a[d]=m;if(B||r&&!r._proxyCallback)r._proxyCallback=function(){return this._bubbleEvent.call(this,d,r,arguments)},r.on("all",r._proxyCallback,this)}a.hasOwnProperty(d)&&(b=a[d],e=this.attributes[d],b?(b.parents=b.parents||[],-1==h.indexOf(b.parents,this)&&b.parents.push(this)):e&&0<e.parents.length&&(e.parents=h.difference(e.parents,[this]),e._proxyCallback&&e.off("all",e._proxyCallback,this)))},this);return u.set.call(this,a,c)},_bubbleEvent:function(a,
|
||||
c,d){var b=d[0].split(":"),f=b[0],e="nested-change"==d[0],l=d[1],p=d[2],k=-1,q=c._proxyCalls,g,t=-1!==h.indexOf(C,f);if(!e){1<h.size(b)&&(g=b[1]);-1!==h.indexOf(E,f)&&(p=l);if(c instanceof n&&t&&l){var m=A(g),r=h.initial(m);(b=c.find(function(a){if(l===a)return!0;if(!a)return!1;var b=a.get(r);if((b instanceof s||b instanceof n)&&l===b)return!0;b=a.get(m);if((b instanceof s||b instanceof n)&&l===b||b instanceof n&&p&&p===b)return!0}))&&(k=c.indexOf(b))}g=a+(-1===k||"change"!==f&&!g?"":"["+k+"]")+(g?
|
||||
"."+g:"");if(/\[\*\]/g.test(g))return this;b=g.replace(/\[\d+\]/g,"[*]");k=[];k.push.apply(k,d);k[0]=f+":"+g;q=c._proxyCalls=q||{};if(this._isEventAvailable.call(this,q,g))return this;q[g]=!0;"change"===f&&(this._previousAttributes[a]=c._previousAttributes,this.changed[a]=c);this.trigger.apply(this,k);"change"===f&&this.get(g)!=d[2]&&this.trigger.apply(this,["nested-change",g,d[1]]);q&&g&&delete q[g];g!==b&&(k[0]=f+":"+b,this.trigger.apply(this,k));return this}},_isEventAvailable:function(a,c){return h.find(a,
|
||||
function(a,b){return-1!==c.indexOf(b,c.length-b.length)})},_createCollection:function(a){var c=a;h.isString(c)&&(c=x(c));if(c&&c.prototype instanceof s)a=new n,a.model=c;else throw Error("type must inherit from Backbone.AssociatedModel");return a},_processPendingEvents:function(){this.visited||(this.visited=!0,this._deferEvents=!1,h.each(this._pendingEvents,function(a){a.c.trigger.apply(a.c,a.a)}),this._pendingEvents=[],h.each(this.relations,function(a){(a=this.attributes[a.key])&&a._processPendingEvents()},
|
||||
this),delete this.visited)},trigger:function(a){this._deferEvents?(this._pendingEvents=this._pendingEvents||[],this._pendingEvents.push({c:this,a:arguments})):u.trigger.apply(this,arguments)},toJSON:function(a){var c,d;this.visited||(this.visited=!0,c=u.toJSON.apply(this,arguments),this.relations&&h.each(this.relations,function(b){var f=this.attributes[b.key];f&&(d=f.toJSON(a),c[b.key]=h.isArray(d)?h.compact(d):d)},this),delete this.visited);return c},clone:function(){return new this.constructor(this.toJSON())},
|
||||
cleanup:function(){h.each(this.relations,function(a){(a=this.attributes[a.key])&&(a.parents=h.difference(a.parents,[this]))},this);this.off()},_getAttr:function(a){var c=this;a=A(a);var d,b;if(!(1>h.size(a))){for(b=0;b<a.length;b++){d=a[b];if(!c)break;c=c instanceof n?isNaN(d)?void 0:c.at(d):c.attributes[d]}return c}}});var G=/[^\.\[\]]+/g,A=function(a){return""===a?[""]:h.isString(a)?a.match(G):a||[]},x=function(a){return h.reduce(a.split("."),function(a,d){return a[d]},z)},H=function(a,c,d){var b;
|
||||
h.find(a,function(a){if(b=h.find(a.relations,function(b){return a.get(b.key)===c},this))return!0},this);return b&&b.map?b.map(d):d},y={};h.each(["set","remove","reset"],function(a){y[a]=n.prototype[a];v[a]=function(c,d){this.model.prototype instanceof s&&this.parents&&(arguments[0]=H(this.parents,this,c));return y[a].apply(this,arguments)}});y.trigger=v.trigger;v.trigger=function(a){this._deferEvents?(this._pendingEvents=this._pendingEvents||[],this._pendingEvents.push({c:this,a:arguments})):y.trigger.apply(this,
|
||||
arguments)};v._processPendingEvents=s.prototype._processPendingEvents}).call(this);
|
||||
@@ -0,0 +1,533 @@
|
||||
//
|
||||
// Backbone-associations.js 0.5.1
|
||||
//
|
||||
// (c) 2013 Dhruva Ray, Jaynti Kanani, Persistent Systems Ltd.
|
||||
// Backbone-associations may be freely distributed under the MIT license.
|
||||
// For all details and documentation:
|
||||
// https://github.com/dhruvaray/backbone-associations/
|
||||
//
|
||||
|
||||
// Initial Setup
|
||||
// --------------
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
// Save a reference to the global object (`window` in the browser, `exports`
|
||||
// on the server).
|
||||
var root = this;
|
||||
|
||||
// The top-level namespace. All public Backbone classes and modules will be attached to this.
|
||||
// Exported for the browser and CommonJS.
|
||||
var _, Backbone, BackboneModel, BackboneCollection, ModelProto,
|
||||
CollectionProto, defaultEvents, AssociatedModel, pathChecker,
|
||||
collectionEvents;
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
_ = require('underscore');
|
||||
Backbone = require('backbone');
|
||||
if (typeof exports !== 'undefined') {
|
||||
exports = module.exports = Backbone;
|
||||
}
|
||||
} else {
|
||||
_ = root._;
|
||||
Backbone = root.Backbone;
|
||||
}
|
||||
// Create local reference `Model` prototype.
|
||||
BackboneModel = Backbone.Model;
|
||||
BackboneCollection = Backbone.Collection;
|
||||
ModelProto = BackboneModel.prototype;
|
||||
CollectionProto = BackboneCollection.prototype;
|
||||
pathChecker = /[\.\[\]]+/g;
|
||||
|
||||
// Built-in Backbone `events`.
|
||||
defaultEvents = ["change", "add", "remove", "reset", "sort", "destroy"];
|
||||
collectionEvents = ["reset", "sort"];
|
||||
|
||||
Backbone.Associations = {
|
||||
VERSION:"0.5.1"
|
||||
};
|
||||
|
||||
// Backbone.AssociatedModel
|
||||
// --------------
|
||||
|
||||
//Add `Many` and `One` relations to Backbone Object.
|
||||
Backbone.Associations.Many = Backbone.Many = "Many";
|
||||
Backbone.Associations.One = Backbone.One = "One";
|
||||
Backbone.Associations.Self = Backbone.Self = "Self";
|
||||
// Define `AssociatedModel` (Extends Backbone.Model).
|
||||
AssociatedModel = Backbone.AssociatedModel = Backbone.Associations.AssociatedModel = BackboneModel.extend({
|
||||
// Define relations with Associated Model.
|
||||
relations:undefined,
|
||||
// Define `Model` property which can keep track of already fired `events`,
|
||||
// and prevent redundant event to be triggered in case of cyclic model graphs.
|
||||
_proxyCalls:undefined,
|
||||
|
||||
// Get the value of an attribute.
|
||||
get:function (attr) {
|
||||
var obj = ModelProto.get.call(this, attr);
|
||||
return obj ? obj : this._getAttr.apply(this, arguments);
|
||||
},
|
||||
|
||||
// Set a hash of model attributes on the Backbone Model.
|
||||
set:function (key, value, options) {
|
||||
var attributes, result;
|
||||
// Duplicate backbone's behavior to allow separate key/value parameters,
|
||||
// instead of a single 'attributes' object.
|
||||
if (_.isObject(key) || key == null) {
|
||||
attributes = key;
|
||||
options = value;
|
||||
} else {
|
||||
attributes = {};
|
||||
attributes[key] = value;
|
||||
}
|
||||
result = this._set(attributes, options);
|
||||
// Trigger events which have been blocked until the entire object graph is updated.
|
||||
this._processPendingEvents();
|
||||
return result;
|
||||
|
||||
},
|
||||
|
||||
// Works with an attribute hash and options + fully qualified paths
|
||||
_set:function (attributes, options) {
|
||||
var attr, modelMap, modelId, obj, result = this;
|
||||
if (!attributes) return this;
|
||||
for (attr in attributes) {
|
||||
//Create a map for each unique object whose attributes we want to set
|
||||
modelMap || (modelMap = {});
|
||||
if (attr.match(pathChecker)) {
|
||||
var pathTokens = getPathArray(attr), initials = _.initial(pathTokens),
|
||||
last = pathTokens[pathTokens.length - 1],
|
||||
parentModel = this.get(initials);
|
||||
if (parentModel instanceof AssociatedModel) {
|
||||
obj = modelMap[parentModel.cid] || (modelMap[parentModel.cid] = {'model':parentModel, 'data':{}});
|
||||
obj.data[last] = attributes[attr];
|
||||
}
|
||||
} else {
|
||||
obj = modelMap[this.cid] || (modelMap[this.cid] = {'model':this, 'data':{}});
|
||||
obj.data[attr] = attributes[attr];
|
||||
}
|
||||
}
|
||||
|
||||
if (modelMap) {
|
||||
for (modelId in modelMap) {
|
||||
obj = modelMap[modelId];
|
||||
this._setAttr.call(obj.model, obj.data, options) || (result = false);
|
||||
|
||||
}
|
||||
} else {
|
||||
result = this._setAttr.call(this, attributes, options);
|
||||
}
|
||||
return result;
|
||||
|
||||
},
|
||||
|
||||
// Set a hash of model attributes on the object,
|
||||
// fire Backbone `event` with options.
|
||||
// It maintains relations between models during the set operation.
|
||||
// It also bubbles up child events to the parent.
|
||||
_setAttr:function (attributes, options) {
|
||||
var attr;
|
||||
// Extract attributes and options.
|
||||
options || (options = {});
|
||||
if (options.unset) for (attr in attributes) attributes[attr] = void 0;
|
||||
this.parents = this.parents || [];
|
||||
|
||||
if (this.relations) {
|
||||
// Iterate over `this.relations` and `set` model and collection values
|
||||
// if `relations` are available.
|
||||
_.each(this.relations, function (relation) {
|
||||
var relationKey = relation.key,
|
||||
relatedModel = relation.relatedModel,
|
||||
collectionType = relation.collectionType,
|
||||
map = relation.map,
|
||||
currVal = this.attributes[relationKey],
|
||||
idKey = currVal && currVal.idAttribute,
|
||||
val, relationOptions, data, relationValue, newCtx = false;
|
||||
|
||||
// Call function if relatedModel is implemented as a function
|
||||
if (relatedModel && !(relatedModel.prototype instanceof BackboneModel))
|
||||
relatedModel = _.isFunction(relatedModel) ?
|
||||
relatedModel.call(this, relation, attributes) :
|
||||
relatedModel;
|
||||
|
||||
// Get class if relation and map is stored as a string.
|
||||
if (relatedModel && _.isString(relatedModel)) {
|
||||
relatedModel = (relatedModel === Backbone.Self) ? this.constructor : map2Scope(relatedModel);
|
||||
}
|
||||
collectionType && _.isString(collectionType) && (collectionType = map2Scope(collectionType));
|
||||
map && _.isString(map) && (map = map2Scope(map));
|
||||
// Merge in `options` specific to this relation.
|
||||
relationOptions = relation.options ? _.extend({}, relation.options, options) : options;
|
||||
|
||||
if (attributes[relationKey]) {
|
||||
// Get value of attribute with relation key in `val`.
|
||||
val = _.result(attributes, relationKey);
|
||||
// Map `val` if a transformation function is provided.
|
||||
val = map ? map(val) : val;
|
||||
|
||||
// If `relation.type` is `Backbone.Many`,
|
||||
// Create `Backbone.Collection` with passed data and perform Backbone `set`.
|
||||
if (relation.type === Backbone.Many) {
|
||||
// `collectionType` of defined `relation` should be instance of `Backbone.Collection`.
|
||||
if (collectionType && !collectionType.prototype instanceof BackboneCollection) {
|
||||
throw new Error('collectionType must inherit from Backbone.Collection');
|
||||
}
|
||||
|
||||
if (val instanceof BackboneCollection) {
|
||||
data = val;
|
||||
// Compute whether the context is a new one after this assignment.
|
||||
newCtx = (currVal !== val);
|
||||
} else {
|
||||
// Create a new collection
|
||||
if (!currVal) {
|
||||
data = collectionType ? new collectionType() : this._createCollection(relatedModel);
|
||||
} else {
|
||||
data = currVal;
|
||||
// Setting this flag will prevent events from firing immediately. That way clients
|
||||
// will not get events until the entire object graph is updated.
|
||||
data._deferEvents = true;
|
||||
}
|
||||
// Use Backbone.Collection's `reset` or smart `set` method
|
||||
data[relationOptions.reset ? 'reset' : 'set'](val, relationOptions);
|
||||
}
|
||||
|
||||
} else if (relation.type === Backbone.One && relatedModel) {
|
||||
if (val instanceof AssociatedModel) {
|
||||
data = val;
|
||||
// Compute whether the context is a new one after this assignment.
|
||||
newCtx = (currVal !== val);
|
||||
} else {
|
||||
//Create a new model
|
||||
if (!currVal) {
|
||||
data = new relatedModel(val, relationOptions);
|
||||
} else {
|
||||
//Is the passed in data for the same key?
|
||||
if (currVal && val[idKey] && currVal.get(idKey) === val[idKey]) {
|
||||
// Setting this flag will prevent events from firing immediately. That way clients
|
||||
// will not get events until the entire object graph is updated.
|
||||
currVal._deferEvents = true;
|
||||
// Perform the traditional `set` operation
|
||||
currVal._set(val, relationOptions);
|
||||
data = currVal;
|
||||
} else {
|
||||
data = new relatedModel(val, relationOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
attributes[relationKey] = data;
|
||||
relationValue = data;
|
||||
|
||||
// Add proxy events to respective parents.
|
||||
// Only add callback if not defined or new Ctx has been identified.
|
||||
if (newCtx || (relationValue && !relationValue._proxyCallback)) {
|
||||
relationValue._proxyCallback = function () {
|
||||
return this._bubbleEvent.call(this, relationKey, relationValue, arguments);
|
||||
};
|
||||
relationValue.on("all", relationValue._proxyCallback, this);
|
||||
}
|
||||
|
||||
}
|
||||
//Distinguish between the value of undefined versus a set no-op
|
||||
if (attributes.hasOwnProperty(relationKey)) {
|
||||
//Maintain reverse pointers - a.k.a parents
|
||||
var updated = attributes[relationKey];
|
||||
var original = this.attributes[relationKey];
|
||||
if (updated) {
|
||||
updated.parents = updated.parents || [];
|
||||
(_.indexOf(updated.parents, this) == -1) && updated.parents.push(this);
|
||||
} else if (original && original.parents.length > 0) { // New value is undefined
|
||||
original.parents = _.difference(original.parents, [this]);
|
||||
// Don't bubble to this parent anymore
|
||||
original._proxyCallback && original.off("all", original._proxyCallback, this);
|
||||
}
|
||||
}
|
||||
}, this);
|
||||
}
|
||||
// Return results for `BackboneModel.set`.
|
||||
return ModelProto.set.call(this, attributes, options);
|
||||
},
|
||||
// Bubble-up event to `parent` Model
|
||||
_bubbleEvent:function (relationKey, relationValue, eventArguments) {
|
||||
var args = eventArguments,
|
||||
opt = args[0].split(":"),
|
||||
eventType = opt[0],
|
||||
catch_all = args[0] == "nested-change",
|
||||
eventObject = args[1],
|
||||
colObject = args[2],
|
||||
indexEventObject = -1,
|
||||
_proxyCalls = relationValue._proxyCalls,
|
||||
cargs,
|
||||
eventPath,
|
||||
basecolEventPath,
|
||||
isDefaultEvent = _.indexOf(defaultEvents, eventType) !== -1;
|
||||
|
||||
//Short circuit the listen in to the nested-graph event
|
||||
if (catch_all) return;
|
||||
|
||||
// Change the event name to a fully qualified path.
|
||||
_.size(opt) > 1 && (eventPath = opt[1]);
|
||||
|
||||
if (_.indexOf(collectionEvents, eventType) !== -1) {
|
||||
colObject = eventObject;
|
||||
}
|
||||
|
||||
// Find the specific object in the collection which has changed.
|
||||
if (relationValue instanceof BackboneCollection && isDefaultEvent && eventObject) {
|
||||
var pathTokens = getPathArray(eventPath),
|
||||
initialTokens = _.initial(pathTokens), colModel;
|
||||
|
||||
colModel = relationValue.find(function (model) {
|
||||
if (eventObject === model) return true;
|
||||
if (!model) return false;
|
||||
var changedModel = model.get(initialTokens);
|
||||
|
||||
if ((changedModel instanceof AssociatedModel || changedModel instanceof BackboneCollection)
|
||||
&& eventObject === changedModel)
|
||||
return true;
|
||||
|
||||
changedModel = model.get(pathTokens);
|
||||
|
||||
if ((changedModel instanceof AssociatedModel || changedModel instanceof BackboneCollection)
|
||||
&& eventObject === changedModel)
|
||||
return true;
|
||||
|
||||
if (changedModel instanceof BackboneCollection && colObject
|
||||
&& colObject === changedModel)
|
||||
return true;
|
||||
});
|
||||
colModel && (indexEventObject = relationValue.indexOf(colModel));
|
||||
}
|
||||
|
||||
// Manipulate `eventPath`.
|
||||
eventPath = relationKey + ((indexEventObject !== -1 && (eventType === "change" || eventPath)) ?
|
||||
"[" + indexEventObject + "]" : "") + (eventPath ? "." + eventPath : "");
|
||||
|
||||
// Short circuit collection * events
|
||||
if (/\[\*\]/g.test(eventPath)) return this;
|
||||
basecolEventPath = eventPath.replace(/\[\d+\]/g, '[*]');
|
||||
|
||||
cargs = [];
|
||||
cargs.push.apply(cargs, args);
|
||||
cargs[0] = eventType + ":" + eventPath;
|
||||
|
||||
// If event has been already triggered as result of same source `eventPath`,
|
||||
// no need to re-trigger event to prevent cycle.
|
||||
_proxyCalls = relationValue._proxyCalls = (_proxyCalls || {});
|
||||
if (this._isEventAvailable.call(this, _proxyCalls, eventPath)) return this;
|
||||
|
||||
// Add `eventPath` in `_proxyCalls` to keep track of already triggered `event`.
|
||||
_proxyCalls[eventPath] = true;
|
||||
|
||||
// Set up previous attributes correctly.
|
||||
if ("change" === eventType) {
|
||||
this._previousAttributes[relationKey] = relationValue._previousAttributes;
|
||||
this.changed[relationKey] = relationValue;
|
||||
}
|
||||
|
||||
// Bubble up event to parent `model` with new changed arguments.
|
||||
this.trigger.apply(this, cargs);
|
||||
|
||||
//Only fire for change. Not change:attribute
|
||||
if ("change" === eventType && this.get(eventPath) != args[2]) {
|
||||
this.trigger.apply(this, ["nested-change", eventPath, args[1]]);
|
||||
}
|
||||
|
||||
// Remove `eventPath` from `_proxyCalls`,
|
||||
// if `eventPath` and `_proxyCalls` are available,
|
||||
// which allow event to be triggered on for next operation of `set`.
|
||||
if (_proxyCalls && eventPath) delete _proxyCalls[eventPath];
|
||||
|
||||
// Create a collection modified event with wild-card
|
||||
if (eventPath !== basecolEventPath) {
|
||||
cargs[0] = eventType + ":" + basecolEventPath;
|
||||
this.trigger.apply(this, cargs);
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
// Has event been fired from this source. Used to prevent event recursion in cyclic graphs
|
||||
_isEventAvailable:function (_proxyCalls, path) {
|
||||
return _.find(_proxyCalls, function (value, eventKey) {
|
||||
return path.indexOf(eventKey, path.length - eventKey.length) !== -1;
|
||||
});
|
||||
},
|
||||
|
||||
// Returns New `collection` of type `relation.relatedModel`.
|
||||
_createCollection:function (type) {
|
||||
var collection, relatedModel = type;
|
||||
_.isString(relatedModel) && (relatedModel = map2Scope(relatedModel));
|
||||
// Creates new `Backbone.Collection` and defines model class.
|
||||
if (relatedModel && relatedModel.prototype instanceof AssociatedModel) {
|
||||
collection = new BackboneCollection();
|
||||
collection.model = relatedModel;
|
||||
} else {
|
||||
throw new Error('type must inherit from Backbone.AssociatedModel');
|
||||
}
|
||||
return collection;
|
||||
},
|
||||
|
||||
// Process all pending events after the entire object graph has been updated
|
||||
_processPendingEvents:function () {
|
||||
if (!this.visited) {
|
||||
this.visited = true;
|
||||
|
||||
this._deferEvents = false;
|
||||
|
||||
// Trigger all pending events
|
||||
_.each(this._pendingEvents, function (e) {
|
||||
e.c.trigger.apply(e.c, e.a);
|
||||
});
|
||||
|
||||
this._pendingEvents = [];
|
||||
|
||||
// Traverse down the object graph and call process pending events on sub-trees
|
||||
_.each(this.relations, function (relation) {
|
||||
var val = this.attributes[relation.key];
|
||||
val && val._processPendingEvents();
|
||||
}, this);
|
||||
|
||||
delete this.visited;
|
||||
}
|
||||
},
|
||||
|
||||
// Override trigger to defer events in the object graph.
|
||||
trigger:function (name) {
|
||||
// Defer event processing
|
||||
if (this._deferEvents) {
|
||||
this._pendingEvents = this._pendingEvents || [];
|
||||
// Maintain a queue of pending events to trigger after the entire object graph is updated.
|
||||
this._pendingEvents.push({c:this, a:arguments});
|
||||
} else {
|
||||
ModelProto.trigger.apply(this, arguments);
|
||||
}
|
||||
},
|
||||
|
||||
// The JSON representation of the model.
|
||||
toJSON:function (options) {
|
||||
var json, aJson;
|
||||
if (!this.visited) {
|
||||
this.visited = true;
|
||||
// Get json representation from `BackboneModel.toJSON`.
|
||||
json = ModelProto.toJSON.apply(this, arguments);
|
||||
// If `this.relations` is defined, iterate through each `relation`
|
||||
// and added it's json representation to parents' json representation.
|
||||
if (this.relations) {
|
||||
_.each(this.relations, function (relation) {
|
||||
var attr = this.attributes[relation.key];
|
||||
if (attr) {
|
||||
aJson = attr.toJSON(options);
|
||||
json[relation.key] = _.isArray(aJson) ? _.compact(aJson) : aJson;
|
||||
}
|
||||
}, this);
|
||||
}
|
||||
delete this.visited;
|
||||
}
|
||||
return json;
|
||||
},
|
||||
|
||||
// Create a new model with identical attributes to this one.
|
||||
clone:function () {
|
||||
return new this.constructor(this.toJSON());
|
||||
},
|
||||
|
||||
// Call this if you want to set an `AssociatedModel` to a falsy value like undefined/null directly.
|
||||
// Not calling this will leak memory and have wrong parents.
|
||||
// See test case "parent relations"
|
||||
cleanup:function () {
|
||||
_.each(this.relations, function (relation) {
|
||||
var val = this.attributes[relation.key];
|
||||
val && (val.parents = _.difference(val.parents, [this]));
|
||||
}, this);
|
||||
this.off();
|
||||
},
|
||||
|
||||
// Navigate the path to the leaf object in the path to query for the attribute value
|
||||
_getAttr:function (path) {
|
||||
|
||||
var result = this,
|
||||
//Tokenize the path
|
||||
attrs = getPathArray(path),
|
||||
key,
|
||||
i;
|
||||
if (_.size(attrs) < 1) return;
|
||||
for (i = 0; i < attrs.length; i++) {
|
||||
key = attrs[i];
|
||||
if (!result) break;
|
||||
//Navigate the path to get to the result
|
||||
result = result instanceof BackboneCollection
|
||||
? (isNaN(key) ? undefined : result.at(key))
|
||||
: result.attributes[key];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
var delimiters = /[^\.\[\]]+/g;
|
||||
|
||||
// Tokenize the fully qualified event path
|
||||
var getPathArray = function (path) {
|
||||
if (path === '') return [''];
|
||||
return _.isString(path) ? (path.match(delimiters)) : path || [];
|
||||
};
|
||||
|
||||
var map2Scope = function (path) {
|
||||
return _.reduce(path.split('.'), function (memo, elem) {
|
||||
return memo[elem]
|
||||
}, root);
|
||||
};
|
||||
|
||||
//Infer the relation from the collection's parents and find the appropriate map for the passed in `models`
|
||||
var map2models = function (parents, target, models) {
|
||||
var relation;
|
||||
//Iterate over collection's parents
|
||||
_.find(parents, function (parent) {
|
||||
//Iterate over relations
|
||||
relation = _.find(parent.relations, function (rel) {
|
||||
return parent.get(rel.key) === target;
|
||||
}, this);
|
||||
if (relation) return true;//break;
|
||||
}, this);
|
||||
|
||||
//If we found a relation and it has a mapping function
|
||||
if (relation && relation.map) {
|
||||
return relation.map(models)
|
||||
}
|
||||
return models;
|
||||
};
|
||||
|
||||
var proxies = {};
|
||||
// Proxy Backbone collection methods
|
||||
_.each(['set', 'remove', 'reset'], function (method) {
|
||||
proxies[method] = BackboneCollection.prototype[method];
|
||||
|
||||
CollectionProto[method] = function (models, options) {
|
||||
//Short-circuit if this collection doesn't hold `AssociatedModels`
|
||||
if (this.model.prototype instanceof AssociatedModel && this.parents) {
|
||||
//Find a map function if available and perform a transformation
|
||||
arguments[0] = map2models(this.parents, this, models);
|
||||
}
|
||||
return proxies[method].apply(this, arguments);
|
||||
}
|
||||
});
|
||||
|
||||
// Override trigger to defer events in the object graph.
|
||||
proxies['trigger'] = CollectionProto['trigger'];
|
||||
CollectionProto['trigger'] = function (name) {
|
||||
if (this._deferEvents) {
|
||||
this._pendingEvents = this._pendingEvents || [];
|
||||
// Maintain a queue of pending events to trigger after the entire object graph is updated.
|
||||
this._pendingEvents.push({c:this, a:arguments});
|
||||
} else {
|
||||
proxies['trigger'].apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
// Attach process pending event functionality on collections as well. Re-use from `AssociatedModel`
|
||||
CollectionProto._processPendingEvents = AssociatedModel.prototype._processPendingEvents;
|
||||
|
||||
|
||||
}).call(this);
|
||||
@@ -1,52 +1,52 @@
|
||||
{
|
||||
"name":"backbone-associations",
|
||||
"filename": "backbone-associations-min.js",
|
||||
"description":"Create object hierarchies with Backbone models. Respond to hierarchy changes using regular Backbone events",
|
||||
"url":"https://github.com/dhruvaray/backbone-associations/",
|
||||
"homepage":"http://dhruvaray.github.io/backbone-associations/",
|
||||
"keywords":["Backbone", "association", "associated", "nested", "model", "cyclic graph", "qualified event paths","relational","relations"],
|
||||
"author":["Dhruva Ray", "Jaynti Kanani"],
|
||||
"dependencies":{
|
||||
"underscore":">=1.4.3",
|
||||
"backbone":">=0.9.10"
|
||||
},
|
||||
"devDependencies":{
|
||||
"phantomjs":"0.2.2"
|
||||
},
|
||||
"jam": {
|
||||
"dependencies": {
|
||||
"underscore":">=1.4.3",
|
||||
"backbone":">=0.9.10"
|
||||
},
|
||||
"main":"backbone-associations.js",
|
||||
"include": [
|
||||
"backbone-associations.js",
|
||||
"backbone-associations-min.js",
|
||||
"README.md",
|
||||
"CHANGELOG.md"
|
||||
]
|
||||
},
|
||||
"scripts":{
|
||||
"test":"phantomjs test/lib/runner.js test/test-suite.html?noglobals=true"
|
||||
},
|
||||
"main":"backbone-associations.js",
|
||||
"version":"0.5.0",
|
||||
"repository":"git://github.com/dhruvaray/backbone-associations.git",
|
||||
"licenses": [
|
||||
{
|
||||
"type": "MIT",
|
||||
"url": "https://github.com/dhruvaray/backbone-associations/blob/master/LICENSE.txt"
|
||||
}
|
||||
],
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Dhruva Ray",
|
||||
"web": "https://github.com/dhruvaray"
|
||||
},
|
||||
{
|
||||
"name": "Jaynti Kanani",
|
||||
"web": "https://github.com/jdkanani"
|
||||
}
|
||||
],
|
||||
"github": "https://github.com/dhruvaray/backbone-associations/"
|
||||
}
|
||||
{
|
||||
"name":"backbone-associations",
|
||||
"filename": "backbone-associations-min.js",
|
||||
"description":"Create object hierarchies with Backbone models. Respond to hierarchy changes using regular Backbone events",
|
||||
"url":"https://github.com/dhruvaray/backbone-associations/",
|
||||
"homepage":"http://dhruvaray.github.io/backbone-associations/",
|
||||
"keywords":["Backbone", "association", "associated", "nested", "model", "cyclic graph", "qualified event paths","relational","relations"],
|
||||
"author":["Dhruva Ray", "Jaynti Kanani"],
|
||||
"dependencies":{
|
||||
"underscore":">=1.4.3",
|
||||
"backbone":">=0.9.10"
|
||||
},
|
||||
"devDependencies":{
|
||||
"phantomjs":"0.2.2"
|
||||
},
|
||||
"jam": {
|
||||
"dependencies": {
|
||||
"underscore":">=1.4.3",
|
||||
"backbone":">=0.9.10"
|
||||
},
|
||||
"main":"backbone-associations.js",
|
||||
"include": [
|
||||
"backbone-associations.js",
|
||||
"backbone-associations-min.js",
|
||||
"README.md",
|
||||
"CHANGELOG.md"
|
||||
]
|
||||
},
|
||||
"scripts":{
|
||||
"test":"phantomjs test/lib/runner.js test/test-suite.html?noglobals=true"
|
||||
},
|
||||
"main":"backbone-associations.js",
|
||||
"version":"0.5.1",
|
||||
"repository":"git://github.com/dhruvaray/backbone-associations.git",
|
||||
"licenses": [
|
||||
{
|
||||
"type": "MIT",
|
||||
"url": "https://github.com/dhruvaray/backbone-associations/blob/master/LICENSE.txt"
|
||||
}
|
||||
],
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Dhruva Ray",
|
||||
"web": "https://github.com/dhruvaray"
|
||||
},
|
||||
{
|
||||
"name": "Jaynti Kanani",
|
||||
"web": "https://github.com/jdkanani"
|
||||
}
|
||||
],
|
||||
"github": "https://github.com/dhruvaray/backbone-associations/"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user