Skip to content

Commit

Permalink
Update map2models to use the new map signature + a test case to demon…
Browse files Browse the repository at this point in the history
…strate usage - discussion #51
  • Loading branch information
dhruvaray committed Aug 9, 2013
1 parent ed07701 commit 07f7a1c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
9 changes: 6 additions & 3 deletions backbone-associations.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,22 @@

//Infer the relation from the collection's parents and find the appropriate map for the passed in `models`
var map2models = function (parents, target, models) {
var relation;
var relation, surrogate;
//Iterate over collection's parents
_.find(parents, function (parent) {
//Iterate over relations
relation = _.find(parent.relations, function (rel) {
return parent.get(rel.key) === target;
}, this);
if (relation) return true;//break;
if (relation) {
surrogate = parent;//surrogate for transformation
return true;//break;
}
}, this);

//If we found a relation and it has a mapping function
if (relation && relation.map) {
return relation.map(models)
return relation.map.call(surrogate, models, target);
}
return models;
};
Expand Down
15 changes: 11 additions & 4 deletions test/associated-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ $(document).ready(function () {
});


test("Polymorphic associations + map: Issue#54", 7, function () {
test("Polymorphic associations + map: Issue#54", 9, function () {
var Fruit = Backbone.AssociatedModel.extend();
var Banana = Fruit.extend();
var Tomato = Fruit.extend();
Expand All @@ -1157,18 +1157,19 @@ $(document).ready(function () {
fruitStore['Banana'] = [
new Banana({species:"Robusta", id:3}),
new Banana({species:"Yallaki", id:4}),
new Banana({species:"Genetic Modification", id:5}),
new Banana({species:"Organic", id:6})
];
fruitStore['Tomato'] = [
new Tomato({species:"Cherry", id:3}),
new Tomato({species:"Regular", id:4}),
new Tomato({species:"Regular", id:4})
];


//Handles both an array of ids and an id
var lazy = function (fids, type) {
fids = _.isArray(fids) ? fids : [fids];


type = type instanceof Backbone.Collection ? type.model : type;
var store = function (type) {
if (type == Banana)
return fruitStore['Banana'];
Expand Down Expand Up @@ -1222,6 +1223,12 @@ $(document).ready(function () {
equal(bananaExplosion.get('fruitable').at(0).get('species') === "Robusta", true);
equal(bananaExplosion.get('fruitable').at(1).get('species') === "Yallaki", true);

bananaExplosion.get('fruitable').add([5, 6]);

equal(bananaExplosion.get('fruitable').at(2).get('species') === "Genetic Modification", true);
equal(bananaExplosion.get('fruitable').at(3).get('species') === "Organic", true);


var tomatoExplosion = new FruitExplosion({fruitable_type:Tomato, fruitable:[3, 4]});
equal(tomatoExplosion.get('fruitable').at(0).get('species') === "Cherry", true);
equal(tomatoExplosion.get('fruitable').at(1).get('species') === "Regular", true);
Expand Down

0 comments on commit 07f7a1c

Please sign in to comment.