-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow passing polymorphic models to collections #73
Conversation
I am not sure why we need this... The current functionality can handle your scenario. Do keep in mind that Backbone Collections are homogeneous bags (even though you may use polymorphism to determine it's type at instantiation or later) The code snippet below should handle your scenario. Let me know if I am missing something? var polymorphic = function (relation, attributes) {
var models = attributes[relation.key];
// Pick any model to infer type of collection
var amodel = (models && models.length) ? models[0] : attributes;
// In case folks pass in Models directly
if ( amodel instanceof Backbone.Model) return amodel;
return amodel ? ( amodel.isBranch ? Tree : Leaf ): Tree;
};
var Tree = Backbone.AssociatedModel.extend({
relations: [{
type: Backbone.Many,
key: 'tree',
relatedModel:polymorphic
}]
});
var Leaf = Backbone.AssociatedModel.extend({
});
var t = new Tree({tree:[{isBranch:true},{isBranch:true}]});
equal(t.get('tree').at(0) instanceof Tree, true) //true
equal(t.get('tree').at(1) instanceof Tree, true) //true
var t1 = new Tree({tree:[{isBranch:false},{isBranch:false}]});
equal(t1.get('tree').at(0) instanceof Tree, false) //true
equal(t1.get('tree').at(1) instanceof Tree, false) //true |
I think the difference is that Backbone Collections don't have to be homogenous, unless I am misunderstanding you. Below is a snippet from Chrome's Web Inspect of an AssociatedModel (with a 1:M relation for
You can see the collection contains models of type |
This can be handled with out-of-the-box backbone. In a recent project I had a collection of All I did was something like this:
|
@blocka I agree Backbone.Collection supports polymorphic models out of the box. This PR lets 1:M relations take advantage of that functionality. At the moment, Backbone-associations will execute whatever function you pass to relations and then assign a single model (that must be another AssociatedModel, not a function) to Backbone.Collection's |
I guess if you don't have a strongly-typed collection on hand (like I did), this would be a useful feature. |
@dhruvaray Added a simple test case. Tried to follow the coding style as closely as possible, but I wrote it pretty late, so apologies if anything is off. |
Allow passing polymorphic models to collections
Wonderful! Thank you so much! Much appreciated. |
Now available as a recipe on our online documentation : http://dhruvaray.github.io/backbone-associations/recipes.html#tut-pm |
Allow polymorphic models to be used in a collection. This can be done by simply passing a function to
relatedModel
that returns the function you would normally set to a Backbone collection'smodel
attribute.This is particularly useful when you're working with a tree with multiple models in it, as in the coffeescript example below: