backbone-relations
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -26,4 +26,3 @@ (function () { | ||
if (rel.hasOne) { | ||
instance = rel.instance = new rel.hasOne(); | ||
instance.set(instance.idAttribute, this.get(rel.fk)); | ||
this._setOne(rel, instance = new rel.hasOne()); | ||
} else { | ||
@@ -40,5 +39,9 @@ instance = rel.instance = new rel.hasMany(); | ||
} else { | ||
instance.each(function (model) { model.set(rel.fk, self.id); }); | ||
this.listenTo(instance, 'add', function (model) { | ||
if (model.get(rel.fk) !== this.id) model.set(rel.fk, this.id); | ||
model.set(rel.fk, this.id); | ||
}); | ||
this.on('change:' + this.idAttribute, function (__, val) { | ||
instance.each(function (model) { model.set(rel.fk, val); }); | ||
}); | ||
} | ||
@@ -66,6 +69,7 @@ } | ||
if (rel.hasOne) { | ||
var instance = this.get(key); | ||
var model = val instanceof rel.hasOne ? val.attributes : val; | ||
instance.set(model, options); | ||
if (this.get(rel.fk) !== instance.id) attrs[rel.fk] = instance.id; | ||
if (val instanceof rel.hasOne) { | ||
this._setOne(rel, val, options); | ||
} else { | ||
this.get(key).set(val, options); | ||
} | ||
} else { | ||
@@ -78,2 +82,17 @@ var models = val instanceof rel.hasMany ? val.models : val; | ||
return set.call(this, attrs, options); | ||
}, | ||
_setOne: function (rel, instance, options) { | ||
if (rel.listener) this.stopListening(null, null, rel.listener); | ||
rel.instance = instance; | ||
var idAttr = instance.idAttribute; | ||
if (instance.id) { | ||
this.set(rel.fk, instance.id, options); | ||
} else { | ||
instance.set(idAttr, this.get(rel.fk), options); | ||
} | ||
rel.listener = function (__, val, options) { | ||
this.set(rel.fk, val, options); | ||
}; | ||
this.listenTo(instance, 'change:' + idAttr, rel.listener); | ||
} | ||
@@ -80,0 +99,0 @@ }); |
{ | ||
"name": "backbone-relations", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"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
5230
90