backbone-relations
Advanced tools
Comparing version 0.0.13 to 0.0.14
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var bind, _; | ||
var Backbone, getCtor, hasMany, hasManyVia, hasOne, hook, initialize, _, _base; | ||
_ = this._ || require('underscore'); | ||
_ = (typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore'); | ||
(typeof module !== "undefined" && module !== null ? module : {}).exports = bind = function(Backbone) { | ||
var getCtor, hasMany, hasManyVia, hasOne, hook, initialize, _base; | ||
getCtor = function(val) { | ||
if (val instanceof Backbone.Model) { | ||
return val; | ||
Backbone = (typeof window !== "undefined" && window !== null ? window.Backbone : void 0) || require('backbone'); | ||
getCtor = function(val) { | ||
if (val instanceof Backbone.Model) { | ||
return val; | ||
} else { | ||
return val(); | ||
} | ||
}; | ||
hook = function(model) { | ||
var get, name, rel, set, _ref, _ref1, _ref2, _results; | ||
if (!model.relations) { | ||
return; | ||
} | ||
_ref = [ | ||
model.get, function() { | ||
return get.apply(model, arguments); | ||
} | ||
], get = _ref[0], model.get = _ref[1]; | ||
_ref1 = [ | ||
model.set, function() { | ||
return set.apply(model, arguments); | ||
} | ||
], set = _ref1[0], model.set = _ref1[1]; | ||
_ref2 = model.relations; | ||
_results = []; | ||
for (name in _ref2) { | ||
rel = _ref2[name]; | ||
if (rel.hasOne) { | ||
_results.push(hasOne(model, name, rel)); | ||
} else if (rel.via) { | ||
_results.push(hasManyVia(model, name, rel)); | ||
} else { | ||
return val(); | ||
_results.push(hasMany(model, name, rel)); | ||
} | ||
}; | ||
hook = function(model) { | ||
var get, name, rel, set, _ref, _ref1, _ref2, _results; | ||
if (!model.relations) { | ||
return; | ||
} | ||
return _results; | ||
}; | ||
hasOne = function(model, name, rel) { | ||
var changeId, ctor, destroy, mine; | ||
ctor = getCtor(rel.hasOne); | ||
mine = rel.myFk; | ||
model.set[name] = function(next) { | ||
var prev; | ||
if (next === (prev = model.get[name])) { | ||
return model; | ||
} | ||
_ref = [ | ||
model.get, function() { | ||
return get.apply(model, arguments); | ||
} | ||
], get = _ref[0], model.get = _ref[1]; | ||
_ref1 = [ | ||
model.set, function() { | ||
return set.apply(model, arguments); | ||
} | ||
], set = _ref1[0], model.set = _ref1[1]; | ||
_ref2 = model.relations; | ||
_results = []; | ||
for (name in _ref2) { | ||
rel = _ref2[name]; | ||
if (rel.hasOne) { | ||
_results.push(hasOne(model, name, rel)); | ||
} else if (rel.via) { | ||
_results.push(hasManyVia(model, name, rel)); | ||
} else { | ||
_results.push(hasMany(model, name, rel)); | ||
} | ||
if (prev) { | ||
model.stopListening(prev, { | ||
'change:id': changeId, | ||
destroy: destroy | ||
}); | ||
} | ||
return _results; | ||
model.set(mine, next != null ? next.id : void 0).get[name] = next; | ||
if (next) { | ||
model.listenTo(next, { | ||
'change:id': changeId, | ||
destroy: destroy | ||
}); | ||
} | ||
return model; | ||
}; | ||
hasOne = function(model, name, rel) { | ||
var changeId, ctor, destroy, mine; | ||
ctor = getCtor(rel.hasOne); | ||
mine = rel.myFk; | ||
model.set[name] = function(next) { | ||
var prev; | ||
if (next === (prev = model.get[name])) { | ||
return model; | ||
} | ||
if (prev) { | ||
model.stopListening(prev, { | ||
'change:id': changeId, | ||
destroy: destroy | ||
}); | ||
} | ||
model.set(mine, next != null ? next.id : void 0).get[name] = next; | ||
if (next) { | ||
model.listenTo(next, { | ||
'change:id': changeId, | ||
destroy: destroy | ||
}); | ||
} | ||
return model; | ||
}; | ||
model.listenTo(ctor.cache(), 'add', function(other) { | ||
if (other.id && other.id == model.get(mine)) { | ||
return this.set[name](other); | ||
} | ||
}); | ||
model.on("change:" + mine, function(__, val) { | ||
return this.set[name](ctor.cache().get(val)); | ||
}); | ||
changeId = function(__, val) { | ||
return model.set(mine, val); | ||
}; | ||
destroy = function() { | ||
model.set[name](null); | ||
if (rel.romeo) { | ||
return model.trigger('destroy', model, model.collection); | ||
} | ||
}; | ||
return model.set[name](ctor.cache().get(model.get(mine))); | ||
}; | ||
hasMany = function(model, name, rel) { | ||
var ctor, models, theirs; | ||
ctor = getCtor(rel.hasMany); | ||
theirs = rel.theirFk; | ||
models = model.get[name] = new ctor.Collection; | ||
models.url = function() { | ||
return "" + (_.result(model, 'url')) + (_.result(rel, 'url') || ("/" + name)); | ||
}; | ||
(models.filters = {})[theirs] = model; | ||
models.listenTo(ctor.cache(), "add change:" + theirs, function(other) { | ||
if (model.id && model.id == other.get(theirs)) { | ||
return this.add(other); | ||
} | ||
}); | ||
models.on("change:" + theirs, function(other) { | ||
return this.remove(other); | ||
}); | ||
if (model.id) { | ||
return models.add(ctor.cache().filter(function(other) { | ||
return model.id == other.get(theirs); | ||
})); | ||
model.listenTo(ctor.cache(), 'add', function(other) { | ||
if (other.id && other.id == model.get(mine)) { | ||
return this.set[name](other); | ||
} | ||
}); | ||
model.on("change:" + mine, function(__, val) { | ||
return this.set[name](ctor.cache().get(val)); | ||
}); | ||
changeId = function(__, val) { | ||
return model.set(mine, val); | ||
}; | ||
hasManyVia = function(model, name, rel) { | ||
var ctor, mine, models, theirs, via, viaCtor, | ||
_this = this; | ||
ctor = getCtor(rel.hasMany); | ||
viaCtor = getCtor(rel.via); | ||
mine = rel.myViaFk; | ||
theirs = rel.theirViaFk; | ||
models = model.get[name] = new ctor.Collection; | ||
models.url = function() { | ||
return "" + (_.result(model, 'url')) + (_.result(rel, 'url') || ("/" + name)); | ||
}; | ||
models.mine = theirs; | ||
via = models.via = new viaCtor.Collection; | ||
via.url = function() { | ||
return "" + (_.result(model, 'url')) + (_.result(viaCtor.Collection.prototype, 'url')); | ||
}; | ||
(via.filters = {})[mine] = model; | ||
via.listenTo(viaCtor.cache(), "add change:" + mine, function(other) { | ||
if (model.id && model.id == other.get(mine)) { | ||
return this.add(other); | ||
} | ||
}); | ||
via.on("change:" + mine, function(other, val) { | ||
return this.remove(other); | ||
}); | ||
models.listenTo(via, 'add', function(other) { | ||
if (other = ctor.cache().get(other.get(theirs))) { | ||
return this.add(other); | ||
} | ||
}); | ||
models.listenTo(via, 'remove', function(other) { | ||
return this.remove(ctor.cache().get(other.get(theirs))); | ||
}); | ||
if (model.id) { | ||
return via.add(viaCtor.cache().filter(function(other) { | ||
return model.id == other.get(mine); | ||
})); | ||
destroy = function() { | ||
model.set[name](null); | ||
if (rel.romeo) { | ||
return model.trigger('destroy', model, model.collection); | ||
} | ||
}; | ||
_.extend(Backbone.Model, { | ||
cache: function() { | ||
return this._cache || (this._cache = new this.Collection); | ||
}, | ||
"new": function(attrs) { | ||
var model; | ||
model = this.cache().get(this.prototype._generateId(attrs)); | ||
return (model != null ? model.set.apply(model, arguments) : void 0) || (function(func, args, ctor) { | ||
ctor.prototype = func.prototype; | ||
var child = new ctor, result = func.apply(child, args); | ||
return Object(result) === result ? result : child; | ||
})(this, arguments, function(){}); | ||
return model.set[name](ctor.cache().get(model.get(mine))); | ||
}; | ||
hasMany = function(model, name, rel) { | ||
var ctor, models, theirs; | ||
ctor = getCtor(rel.hasMany); | ||
theirs = rel.theirFk; | ||
models = model.get[name] = new ctor.Collection; | ||
models.url = function() { | ||
return "" + (_.result(model, 'url')) + (_.result(rel, 'url') || ("/" + name)); | ||
}; | ||
(models.filters = {})[theirs] = model; | ||
models.listenTo(ctor.cache(), "add change:" + theirs, function(other) { | ||
if (model.id && model.id == other.get(theirs)) { | ||
return this.add(other); | ||
} | ||
}); | ||
initialize = Backbone.Model.prototype.initialize; | ||
_.extend(Backbone.Model.prototype, { | ||
initialize: function(attrs, options) { | ||
initialize.apply(this, arguments); | ||
if ((options != null ? options.cache : void 0) != null) { | ||
this.cache = options.cache; | ||
} | ||
if (this.cache) { | ||
this.constructor.cache().add(this); | ||
} | ||
return hook(this); | ||
}, | ||
via: function(rel, id) { | ||
var attrs, viaCtor; | ||
if (!(id = (id != null ? id.id : void 0) || id)) { | ||
return; | ||
} | ||
viaCtor = getCtor(this.relations[rel].via); | ||
(attrs = {})[this.relations[rel].myViaFk] = this.id; | ||
attrs[this.relations[rel].theirViaFk] = id; | ||
return this.get[rel].via.get(viaCtor.prototype._generateId(attrs)); | ||
models.on("change:" + theirs, function(other) { | ||
return this.remove(other); | ||
}); | ||
if (model.id) { | ||
return models.add(ctor.cache().filter(function(other) { | ||
return model.id == other.get(theirs); | ||
})); | ||
} | ||
}; | ||
hasManyVia = function(model, name, rel) { | ||
var ctor, mine, models, theirs, via, viaCtor, | ||
_this = this; | ||
ctor = getCtor(rel.hasMany); | ||
viaCtor = getCtor(rel.via); | ||
mine = rel.myViaFk; | ||
theirs = rel.theirViaFk; | ||
models = model.get[name] = new ctor.Collection; | ||
models.url = function() { | ||
return "" + (_.result(model, 'url')) + (_.result(rel, 'url') || ("/" + name)); | ||
}; | ||
models.mine = theirs; | ||
via = models.via = new viaCtor.Collection; | ||
via.url = function() { | ||
return "" + (_.result(model, 'url')) + (_.result(viaCtor.Collection.prototype, 'url')); | ||
}; | ||
(via.filters = {})[mine] = model; | ||
via.listenTo(viaCtor.cache(), "add change:" + mine, function(other) { | ||
if (model.id && model.id == other.get(mine)) { | ||
return this.add(other); | ||
} | ||
}); | ||
(_base = Backbone.Model.prototype)._generateId || (_base._generateId = function(attrs) { | ||
if (attrs == null) { | ||
attrs = this.attributes; | ||
via.on("change:" + mine, function(other, val) { | ||
return this.remove(other); | ||
}); | ||
models.listenTo(via, 'add', function(other) { | ||
if (other = ctor.cache().get(other.get(theirs))) { | ||
return this.add(other); | ||
} | ||
return attrs[this.idAttribute]; | ||
}); | ||
return Backbone; | ||
models.listenTo(via, 'remove', function(other) { | ||
return this.remove(ctor.cache().get(other.get(theirs))); | ||
}); | ||
if (model.id) { | ||
return via.add(viaCtor.cache().filter(function(other) { | ||
return model.id == other.get(mine); | ||
})); | ||
} | ||
}; | ||
if (this.Backbone) { | ||
bind(Backbone); | ||
} | ||
_.extend(Backbone.Model, { | ||
cache: function() { | ||
return this._cache || (this._cache = new this.Collection); | ||
}, | ||
"new": function(attrs) { | ||
var model; | ||
model = this.cache().get(this.prototype._generateId(attrs)); | ||
return (model != null ? model.set.apply(model, arguments) : void 0) || (function(func, args, ctor) { | ||
ctor.prototype = func.prototype; | ||
var child = new ctor, result = func.apply(child, args); | ||
return Object(result) === result ? result : child; | ||
})(this, arguments, function(){}); | ||
} | ||
}); | ||
initialize = Backbone.Model.prototype.initialize; | ||
_.extend(Backbone.Model.prototype, { | ||
initialize: function(attrs, options) { | ||
initialize.apply(this, arguments); | ||
if ((options != null ? options.cache : void 0) != null) { | ||
this.cache = options.cache; | ||
} | ||
if (this.cache) { | ||
this.constructor.cache().add(this); | ||
} | ||
return hook(this); | ||
}, | ||
via: function(rel, id) { | ||
var attrs, viaCtor; | ||
if (!(id = (id != null ? id.id : void 0) || id)) { | ||
return; | ||
} | ||
viaCtor = getCtor(this.relations[rel].via); | ||
(attrs = {})[this.relations[rel].myViaFk] = this.id; | ||
attrs[this.relations[rel].theirViaFk] = id; | ||
return this.get[rel].via.get(viaCtor.prototype._generateId(attrs)); | ||
} | ||
}); | ||
(_base = Backbone.Model.prototype)._generateId || (_base._generateId = function(attrs) { | ||
if (attrs == null) { | ||
attrs = this.attributes; | ||
} | ||
return attrs[this.idAttribute]; | ||
}); | ||
}).call(this); |
{ | ||
"name": "backbone-relations", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"author": "Casey Foster <casey@caseywebdev.com>", | ||
@@ -5,0 +5,0 @@ "licence": "MIT", |
@@ -16,11 +16,8 @@ backbone-relations [![Build Status](https://secure.travis-ci.org/caseywebdev/backbone-relations.png)](http://travis-ci.org/caseywebdev/backbone-relations) | ||
This plugin extends the Backbone core, so you'll need to pass in your instance of Backbone. | ||
Include the file in your browser or for Node: | ||
```coffee | ||
Backbone = require 'backbone' | ||
require('backbone-relations') Backbone | ||
require 'backbone-relations' | ||
``` | ||
This is done automatically in the browser. | ||
Check out the tests for examples. | ||
@@ -34,24 +31,1 @@ | ||
``` | ||
Licence | ||
------- | ||
Copyright (C) 2012 Casey Foster <casey@caseywebdev.com> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8409
190
30