Created
April 12, 2012 05:05
-
-
Save diurnalist/2364703 to your computer and use it in GitHub Desktop.
Lightweight Relation Modeling with Backbone (5)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function delegateModelEvents(from, to, eventKey) { | |
from.bind('all', function(eventName) { | |
var args = _.toArray(arguments); | |
if (eventKey) { | |
args[0] = eventKey + ':' + args[0]; | |
} | |
to.trigger.apply(to, args); | |
}); | |
} | |
function getUpdateOp(model) { | |
return (model instanceof Backbone.Collection) ? 'reset' : 'set'; | |
} | |
Backbone.RelationalModel = Backbone.Model.extend({ | |
relations: {}, | |
set: function(attrs, options) { | |
_.each(this.relations, function(constructor, key) { | |
var relation = this[key]; | |
// set up relational model if it's not there yet | |
if (!relation) { | |
relation = this[key] = new constructor(); | |
// makes it so relation events are triggered out | |
// e.g. 'add' on a relation called 'collection' would | |
// trigger event 'collection:add' on this model | |
delegateModelEvents(relation, this, key); | |
} | |
// check to see if incoming set will affect relation | |
if (attrs[key]) { | |
// perform update on relation model | |
relation[getUpdateOp(relation)](attrs[key], options); | |
// remove from attr hash, prevents duplication of data + | |
// keeps models out of attributes, which should be only used for | |
// dumb JSON attributes | |
delete attrs[key]; | |
} | |
}, this); | |
return Backbone.Model.prototype.set.call(this, attrs, options); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment