Skip to content

Commit

Permalink
make collectiontype and relatedModel behavior uniform - issue # 102
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvaray committed Feb 13, 2014
1 parent 040e3f1 commit 44f1af2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
22 changes: 17 additions & 5 deletions backbone-associations.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,11 @@
this.constructor :
map2Scope(relatedModel, activationContext);
}
collectionType && _.isString(collectionType) &&
(collectionType = map2Scope(collectionType, activationContext));

map && _.isString(map) && (map = map2Scope(map, activationContext));
// Merge in `options` specific to this relation.
relationOptions = relation.options ? _.extend({}, relation.options, options) : options;

if ((!relatedModel) && (!collectionType))
throw new Error('specify either a relatedModel or collectionType');

if (attributes[relationKey]) {
// Get value of attribute with relation key in `val`.
val = _.result(attributes, relationKey);
Expand All @@ -267,6 +263,22 @@
// If `relation.type` is `Backbone.Many`,
// Create `Backbone.Collection` with passed data and perform Backbone `set`.
if (relation.type === Backbone.Many) {

if (collectionType && _.isFunction(collectionType) &&
(collectionType.prototype instanceof BackboneModel))
throw new Error('type is of Backbone.Model. Specify derivatives of Backbone.Collection');

// Call function if collectionType is implemented as a function
if (collectionType && !(collectionType.prototype instanceof BackboneCollection))
collectionType = _.isFunction(collectionType) ?
collectionType.call(this, relation, attributes) : collectionType;

collectionType && _.isString(collectionType) &&
(collectionType = map2Scope(collectionType, activationContext));

if ((!relatedModel) && (!collectionType))
throw new Error('specify either a relatedModel or collectionType');

// `collectionType` of defined `relation` should be instance of `Backbone.Collection`.
if (collectionType && !collectionType.prototype instanceof BackboneCollection) {
throw new Error('collectionType must inherit from Backbone.Collection');
Expand Down
60 changes: 59 additions & 1 deletion test/associated-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ $(document).ready(function () {
equal(emp.get("works_for").get('number'), -1, "number has default value");
});

test("invalid relations", 4, function () {
test("invalid relations", 5, function () {
var em1 = Backbone.AssociatedModel.extend({
relations:[
{
Expand Down Expand Up @@ -465,6 +465,24 @@ $(document).ready(function () {
equal(e.message === "specify a relatedModel for Backbone.One type", true)
}

var em4 = Backbone.AssociatedModel.extend({
relations: [
{
type: Backbone.Many,
key: 'parent',
collectionType: em2//RelatedModel specified
}
]
});

try {
var em4i = new em4;
em4i.set('parent', {id: 1});
} catch (e) {
equal(e.message === "type is of Backbone.Model. Specify derivatives of Backbone.Collection", true)
}


var Owner = Backbone.OriginalModel.extend();
var House = Backbone.AssociatedModel.extend({
relations:[
Expand Down Expand Up @@ -2319,6 +2337,46 @@ $(document).ready(function () {

});

test("Make collectionType a function as well - Issue #102", 1, function () {

var Animal = Backbone.AssociatedModel.extend({
relations: [
{
type: Backbone.One,
key: 'livesIn',
relatedModel: function () {
return Zoo;
}
}
]
});

var Animals = Backbone.Collection.extend({
model: Animal
});

var Zoo = Backbone.AssociatedModel.extend({
relations: [
{
type: Backbone.Many,
key: 'animals',
collectionType: function () {
return Animals;
}
}
]
});

var aZoo = new Zoo();
var animals = new Animals([
{species: 'fish', livesIn: aZoo},
{species: 'mamals', livesIn: aZoo}
]);
aZoo.set('animals', animals);
equal(aZoo.get('animals').models.length, 2);

});

test("Do not bubble if possible - Issue #88", 1, function () {

var addAnimals = function (zoo, count, auto) {
Expand Down

0 comments on commit 44f1af2

Please sign in to comment.