backbone-relations
Advanced tools
Comparing version 0.7.5 to 0.7.6
@@ -17,14 +17,14 @@ (function (root, factory) { | ||
var Relation = function (owner, key, options) { | ||
_.extend(this, options); | ||
this.owner = owner; | ||
this.key = key; | ||
if (this.via) { | ||
var split = this.via.split('#'); | ||
this.via = split[0]; | ||
this.viaKey = split[1] || key; | ||
} | ||
}; | ||
var Relation = herit({ | ||
constructor: function (owner, key, options) { | ||
_.extend(this, options); | ||
this.owner = owner; | ||
this.key = key; | ||
if (this.via) { | ||
var split = this.via.split('#'); | ||
this.via = split[0]; | ||
this.viaKey = split[1] || key; | ||
} | ||
}, | ||
_.extend(Relation.prototype, { | ||
get: function () { return this.instance(); }, | ||
@@ -58,12 +58,30 @@ | ||
var fk = this.fk; | ||
var key = this.key; | ||
var reverse = this.reverse; | ||
var instance = this._instance = new Collection({id: owner.get(fk)}); | ||
var idAttr = Model.prototype.idAttribute; | ||
instance.on('add change:' + idAttr, function (model, __, options) { | ||
owner.set(fk, model.id, options); | ||
}); | ||
var attrs = {}; | ||
attrs[idAttr] = owner.get(fk); | ||
var instance = this._instance = new Collection(attrs); | ||
instance | ||
.on('add change:' + idAttr, function (model, __, options) { | ||
owner.set(fk, model.id, options); | ||
}) | ||
.on('all', function (name, model) { | ||
if (name.indexOf('change') !== 0) return; | ||
var options = _.last(arguments); | ||
if (!options.owners) { | ||
options = _.clone(options); | ||
options.owners = {}; | ||
options.owners[model.cid] = true; | ||
} | ||
if (options.owners[owner.cid]) return; | ||
options.owners[owner.cid] = true; | ||
name = name.replace(/^change/, 'change:' + key); | ||
var args = [name].concat([].slice.call(arguments, 1, -1), options); | ||
owner.trigger.apply(owner, args); | ||
}); | ||
owner.on('change:' + fk, function (__, val, options) { | ||
if (instance.first().id !== val) { | ||
instance.set(new Model({id: val}), options); | ||
} | ||
if (instance.first().id === val) return; | ||
attrs[idAttr] = val; | ||
instance.set(new Model(attrs), options); | ||
}); | ||
@@ -70,0 +88,0 @@ if (reverse) { |
{ | ||
"name": "backbone-relations", | ||
"version": "0.7.5", | ||
"version": "0.7.6", | ||
"main": "./backbone-relations.js", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
{ | ||
"name": "backbone-relations", | ||
"version": "0.7.5", | ||
"version": "0.7.6", | ||
"author": "Casey Foster <c@sey.me>", | ||
@@ -5,0 +5,0 @@ "licence": "MIT", |
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
35895
169