Last active
December 22, 2015 03:38
-
-
Save dhruvaray/6411141 to your computer and use it in GitHub Desktop.
System-wide map function (to convert an id or array of ids to the appropriate model instance from model stores). Also demonstrates polymorphic associations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Fruit = Backbone.AssociatedModel.extend(); | |
var Banana = Fruit.extend(); | |
var Tomato = Fruit.extend(); | |
var fruitStore = {}; | |
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}) | |
]; | |
//Determine type at run-time | |
var polymorphic = function (relation, attributes) { | |
var key = relation.key + '_type'; | |
return attributes[key] || this.get(key); | |
}; | |
//Handles both an array of ids and an id. Converts and id (or array of ids) to instances from the fruit store | |
//System wide function. Can handle different fruit types | |
var map = 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']; | |
if (type == Tomato) | |
return fruitStore['Tomato']; | |
}(type); | |
return _.map( | |
fids, | |
function (fid) { | |
return _.findWhere(store, {id:fid}); | |
} | |
); | |
}; | |
var Oatmeal = Backbone.AssociatedModel.extend({ | |
relations:[ | |
{ | |
type:Backbone.One, | |
key:'fruitable', | |
relatedModel:polymorphic, | |
map:map | |
} | |
] | |
}); | |
var aHealthyBowl = new Oatmeal({fruitable_type:Banana, fruitable:{species:"Robusta"}}); | |
//aHealthyBowl.get('fruitable') instanceof Banana == true | |
//aHealthyBowl.get('fruitable') instanceof Tomato == false | |
var aHealthyBowl2 = new Oatmeal({fruitable_type:Banana, fruitable:3}); | |
//aHealthyBowl2.get('fruitable') instanceof Banana == true | |
//Test with Backbone.Many | |
var FruitExplosion = Backbone.AssociatedModel.extend({ | |
relations:[ | |
{ | |
type:Backbone.Many, | |
key:'fruitable', | |
relatedModel:polymorphic, | |
map:map | |
} | |
] | |
}); | |
var bananaExplosion = new FruitExplosion({fruitable_type:Banana, fruitable:[3, 4]}); | |
//bananaExplosion.get('fruitable').at(0).get('species') === "Robusta" | |
//bananaExplosion.get('fruitable').at(1).get('species') === "Yallaki" | |
bananaExplosion.get('fruitable').add([5, 6]); | |
//bananaExplosion.get('fruitable').at(2).get('species') === "Genetic Modification" | |
//bananaExplosion.get('fruitable').at(3).get('species') === "Organic" | |
var tomatoExplosion = new FruitExplosion({fruitable_type:Tomato, fruitable:[3, 4]}); | |
//tomatoExplosion.get('fruitable').at(0).get('species') === "Cherry" | |
//tomatoExplosion.get('fruitable').at(1).get('species') === "Regular" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @monokrome for this example and motivation.