backbone-relations
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -19,2 +19,10 @@ (function () { | ||
this.owner = owner; | ||
var proto = (this.hasOne || this.hasMany.prototype.model).prototype; | ||
if (!proto.relations) return; | ||
var hasOne = !this.hasOne; | ||
var fk = this.fk; | ||
this.reverse = _.reduce(proto.relations(), function (reverse, rel, key) { | ||
if (!rel.via && hasOne !== !rel.hasOne && fk === rel.fk) return key; | ||
return reverse; | ||
}, null); | ||
}; | ||
@@ -27,4 +35,5 @@ | ||
var instance = this.instance; | ||
if (instance && instance.id === this.owner.get(this.fk)) return instance; | ||
this.set(instance = new this.hasOne()); | ||
var fk = this.owner.get(this.fk); | ||
if (instance && instance.id === fk) return instance; | ||
this.set(instance = new this.hasOne({id: fk})); | ||
return instance; | ||
@@ -39,11 +48,9 @@ }, | ||
var fk = this.fk; | ||
var reverse = this.reverse; | ||
var idAttr = val.idAttribute; | ||
if (val.id) { | ||
owner.set(fk, val.id, options); | ||
} else { | ||
val.set(idAttr, owner.get(fk), options); | ||
} | ||
owner.set(fk, val.id, options); | ||
this.listenTo(val, 'change:' + idAttr, function (__, val, options) { | ||
owner.set(fk, val, options); | ||
}); | ||
if (reverse) val.get(reverse).add(owner); | ||
} else { | ||
@@ -70,4 +77,5 @@ this.get(this.key).set(val, options); | ||
var owner = instance.owner = this.owner; | ||
var fk = instance.fk = this.fk; | ||
instance.fk = this.fk; | ||
var key = this.key; | ||
var reverse = this.reverse; | ||
instance.url = function () { | ||
@@ -78,10 +86,4 @@ return _.result(owner, 'url') + (urlRoot || '/' + key); | ||
instance.via = owner.get(this.via.split('#')[0]); | ||
} else { | ||
instance.each(function (model) { model.set(fk, owner.id); }); | ||
owner.listenTo(instance, 'add', function (model) { | ||
model.set(fk, owner.id); | ||
}); | ||
owner.on('change:' + owner.idAttribute, function (__, val) { | ||
instance.each(function (model) { model.set(fk, val); }); | ||
}); | ||
} else if (reverse) { | ||
instance.on('add', function (model) { model.set(reverse, owner); }); | ||
} | ||
@@ -88,0 +90,0 @@ return this.get(); |
{ | ||
"name": "backbone-relations", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"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
6318
122