backbone-relations
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -7,2 +7,3 @@ (function () { | ||
var _ = node ? require('underscore') : window._; | ||
if (node) require('underscore-inherit'); | ||
var Backbone = node ? require('backbone') : window.Backbone; | ||
@@ -15,55 +16,99 @@ | ||
var setHasOne = function (model, rel, instance, options) { | ||
if (rel.instance) rel.instance.off(null, rel.listener); | ||
rel.instance = instance; | ||
var idAttr = instance.idAttribute; | ||
var fk = rel.fk; | ||
if (instance.id) { | ||
model.set(fk, instance.id, options); | ||
} else { | ||
instance.set(idAttr, model.get(fk), options); | ||
} | ||
instance.on('change:' + idAttr, rel.listener = function (__, val, options) { | ||
model.set(fk, val, options); | ||
}); | ||
var Relation = function (owner, key, options) { | ||
_.extend(this, options); | ||
this.key = key; | ||
this.owner = owner; | ||
}; | ||
Backbone.Model = Backbone.Model.extend({ | ||
constructor: function (attrs, options) { | ||
if (this.relations) this.relations = this.relations(); | ||
return constructor.call(this, attrs, options); | ||
_.extend(Relation.prototype, Backbone.Events); | ||
var HasOneRelation = _.inherit(Relation, { | ||
get: function () { | ||
var instance = this.instance; | ||
if (instance && instance.id === this.owner.get(this.fk)) return instance; | ||
this.set(instance = new this.hasOne()); | ||
return instance; | ||
}, | ||
get: function (key) { | ||
var rel = this.relations && this.relations[key]; | ||
if (!rel) return get.call(this, key); | ||
var instance = rel.instance; | ||
if (!instance || (rel.hasOne && instance.id !== this.get(rel.fk))) { | ||
if (rel.hasOne) { | ||
setHasOne(this, rel, instance = new rel.hasOne()); | ||
set: function (val, options) { | ||
if (val instanceof this.hasOne) { | ||
if (this.instance) this.stopListening(this.instance); | ||
this.instance = val; | ||
var owner = this.owner; | ||
var fk = this.fk; | ||
var idAttr = val.idAttribute; | ||
if (val.id) { | ||
owner.set(fk, val.id, options); | ||
} else { | ||
instance = rel.instance = new rel.hasMany(); | ||
instance.owner = this; | ||
instance.fk = rel.fk; | ||
var self = this; | ||
instance.url = function () { | ||
return _.result(self, 'url') + (rel.urlRoot || '/' + key); | ||
}; | ||
if (rel.via) { | ||
instance.via = this.get(rel.via); | ||
} else { | ||
instance.each(function (model) { model.set(rel.fk, self.id); }); | ||
this.listenTo(instance, 'add', function (model) { | ||
model.set(rel.fk, this.id); | ||
}); | ||
this.on('change:' + this.idAttribute, function (__, val) { | ||
instance.each(function (model) { model.set(rel.fk, val); }); | ||
}); | ||
} | ||
val.set(idAttr, owner.get(fk), options); | ||
} | ||
this.listenTo(val, 'change:' + idAttr, function (__, val, options) { | ||
owner.set(fk, val, options); | ||
}); | ||
} else { | ||
this.get(this.key).set(val, options); | ||
} | ||
return instance; | ||
} | ||
}); | ||
var HasManyRelation = _.inherit(Relation, { | ||
get: function () { | ||
var instance = this.instance; | ||
if (instance) { | ||
if (!this.via) return instance; | ||
var models = instance.via.pluck(this.via.split('#')[1] || this.key); | ||
return instance.set( | ||
models[0] instanceof this.hasMany ? | ||
_.flatten(_.pluck(models, 'models')) : | ||
models | ||
); | ||
} | ||
instance = this.instance = new this.hasMany(); | ||
var urlRoot = instance.urlRoot = this.urlRoot; | ||
var owner = instance.owner = this.owner; | ||
var fk = instance.fk = this.fk; | ||
var key = this.key; | ||
instance.url = function () { | ||
return _.result(owner, 'url') + (urlRoot || '/' + key); | ||
}; | ||
if (this.via) { | ||
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); }); | ||
}); | ||
} | ||
return this.get(); | ||
}, | ||
set: function (val, options) { | ||
var models = val instanceof this.hasMany ? val.models : val; | ||
this.get(this.key).set(models, options); | ||
} | ||
}); | ||
Backbone.Model = Backbone.Model.extend({ | ||
constructor: function () { | ||
if (this.relations) { | ||
this.relations = _.reduce(this.relations(), function (o, options, key) { | ||
var ctor = options.hasOne ? HasOneRelation : HasManyRelation; | ||
o[key] = new ctor(this, key, options); | ||
return o; | ||
}, {}, this); | ||
} | ||
return constructor.apply(this, arguments); | ||
}, | ||
get: function (key) { | ||
if (!this.relations) return get.apply(this, arguments); | ||
var rel = this.relations[key]; | ||
return rel ? rel.get(key) : get.call(this, key); | ||
}, | ||
set: function (key, val, options) { | ||
if (!this.relations) return set.apply(this, arguments); | ||
if (key == null) return this; | ||
@@ -77,18 +122,8 @@ var attrs; | ||
} | ||
if (this.relations) { | ||
for (key in attrs) { | ||
var rel = this.relations[key]; | ||
if (!rel) continue; | ||
for (key in attrs) { | ||
var rel = this.relations[key]; | ||
if (rel) { | ||
val = attrs[key]; | ||
delete attrs[key]; | ||
if (rel.hasOne) { | ||
if (val instanceof rel.hasOne) { | ||
setHasOne(this, rel, val, options); | ||
} else { | ||
this.get(key).set(val, options); | ||
} | ||
} else { | ||
var models = val instanceof rel.hasMany ? val.models : val; | ||
this.get(key).set(models, options); | ||
} | ||
rel.set(val, options); | ||
} | ||
@@ -95,0 +130,0 @@ } |
{ | ||
"name": "backbone-relations", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"author": "Casey Foster <c@sey.me>", | ||
@@ -12,3 +12,4 @@ "licence": "MIT", | ||
"dependencies": { | ||
"underscore": "x" | ||
"underscore": "x", | ||
"underscore-inherit": "x" | ||
}, | ||
@@ -15,0 +16,0 @@ "devDependencies": { |
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
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
6167
120
2
3
+ Addedunderscore-inherit@x
+ Addedunderscore-inherit@1.0.1(transitive)