backbone-relations
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -22,14 +22,15 @@ (function () { | ||
_.extend(Relation.prototype, { | ||
set: function (val, options) { | ||
if (!options) options = {}; | ||
if (!options.owner) options.owner = this.owner; | ||
var instance = this.instance(); | ||
instance.set(val, options); | ||
var owner = options.owner; | ||
if (owner instanceof (this.hasOne || this.hasMany.prototype.model)) { | ||
var existing = instance.get(owner); | ||
if (existing && existing !== owner) { | ||
instance.remove(existing, options).add(owner, options); | ||
} | ||
} | ||
get: function () { return this.instance(); }, | ||
set: function (val, options) { this.instance().set(val, options); }, | ||
resolveVia: function () { | ||
if (!this.via) return this.get(); | ||
var split = this.via.split('#'); | ||
var models = this.owner.via(split[0]).pluck(split[1] || this.key); | ||
return new this.hasMany( | ||
models[0] instanceof this.hasMany ? | ||
_.flatten(_.pluck(models, 'models')) : | ||
models | ||
); | ||
} | ||
@@ -52,3 +53,5 @@ }); | ||
owner.on('change:' + fk, function (__, val, options) { | ||
if (!instance.get(val)) instance.set(new Model({id: val}), options); | ||
if (instance.first().id !== val) { | ||
instance.set(new Model({id: val}), options); | ||
} | ||
}); | ||
@@ -84,3 +87,3 @@ if (reverse) { | ||
instance.via = owner.get(this.via.split('#')[0]); | ||
} else if (this.reverse) { | ||
} else if (reverse) { | ||
instance.on('add', function (model, __, options) { | ||
@@ -91,14 +94,2 @@ model.set(reverse, owner, options); | ||
return instance; | ||
}, | ||
get: function () { | ||
var instance = this.instance(); | ||
if (!this.via) return instance; | ||
var split = this.via.split('#'); | ||
var models = this.owner.get(split[0]).pluck(split[1] || this.key); | ||
return instance.set( | ||
models[0] instanceof this.hasMany ? | ||
_.flatten(_.pluck(models, 'models')) : | ||
models | ||
); | ||
} | ||
@@ -127,3 +118,2 @@ }); | ||
set: function (key, val, options) { | ||
if (!this.relations) return set.apply(this, arguments); | ||
if (key == null) return this; | ||
@@ -137,2 +127,3 @@ var attrs; | ||
} | ||
if (!this.relations) return set.call(this, key, val, options); | ||
for (key in attrs) { | ||
@@ -147,2 +138,8 @@ var rel = this.relations[key]; | ||
return set.call(this, attrs, options); | ||
}, | ||
via: function (key) { | ||
var relations = this.relations; | ||
if (!relations || !relations[key]) return; | ||
return relations[key].resolveVia(key); | ||
} | ||
@@ -149,0 +146,0 @@ }); |
{ | ||
"name": "backbone-relations", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"author": "Casey Foster <c@sey.me>", | ||
@@ -5,0 +5,0 @@ "licence": "MIT", |
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
6446
127