Skip to content
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

Supporting polymorphic collections #793

Closed
dnsco opened this issue Dec 16, 2011 · 2 comments
Closed

Supporting polymorphic collections #793

dnsco opened this issue Dec 16, 2011 · 2 comments
Labels

Comments

@dnsco
Copy link

dnsco commented Dec 16, 2011

Howdy!

Right now backbone seems to only support collections of one type, we're working on a stream with many polymorphic types that all quack the same.

It'd be nice to implement this via inheritance(extend), rather than our current technique of creating proxies/mixins in the base class.

backbone collection seems to have a hardcoded new this.model in it,

I was thinking about doing something like:

modelClass : function(){
   return  this[postType]
}

this.model = this.model || this.modelClass()

new this.model

is this a change you would like to have implemented, or are you opposed to it for an ideological reason?

If you want it, I'll implemented, as it will help me on a couple projects I'm working on.

@jashkenas
Copy link
Owner

Fortunately, because of the (in my opinion, buggy) way that JavaScript constructors work, you can already do collections with polymorphic models. Like so:

var Library = Backbone.Collection.extend({

  model: function(attrs, options) {
    if (condition) {
      return new PublicDocument(attrs, options); 
    } else {
      return new PrivateDocument(attrs, options);
    }
  }

});

@dnsco
Copy link
Author

dnsco commented Jan 17, 2012

Thanks dude, I wrote this issue when I was working on a client project, but now Imma integrate it into diaspora and it will be better <3333333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants