-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Adding attributes to Collections? #1442
Comments
Hi @Aaunel, thanks for your issue! You can certainly accomplish what you're describing without any extra support. var Album = Backbone.Collection.extend({
model: Song,
initialize: function(models, options) {
_.extend(this, _.pick(options, 'name', 'publisher'));
}
});
var album = new Album([], {name: 'Greatest Hits', publisher: 'Sony'});
console.log(album.name, album.publisher); // Greatest Hits, Sony However, it sounds like what you want is a separate model for storing album attributes. For instance: var album = new Backbone.Model({name: 'Greatest Hits', publisher: 'Sony'});
album.songs = new Backbone.Collection(...); I think you'll have more success with that pattern. Hope it helps! |
@braddunbar Thanks for the response. I'm currently wrapping anything that needs these attributes (as well as multi-collection data structures) in models. If that's not considered bad-practice, I'll carry on that way? Otherwise, that solution above would work perfectly for small selections of metadata. Cheers! |
👍 @braddunbar , the answer helped :) |
I've looked through the closed 'enhancements' flagged for Backbone, and couldn't find anything on this specific subject, but am wondering whether attributes for Collections is something that's been investigated?
Personally, I could see plenty of utility in being able to provide a small selection of meta data to a collection.
For example, say we have an
Album
collection ofsong
models:the properties attribute could allow something like this:
Otherwise, at the moment we would have something like this, which relies on that collection being populated:
I discovered a similar (old) topic on Stack Overflow: Setting attributes on a collection - backbone js.
Where I can't rely on data being available in a collection, I'm actually having to wrap the collection in a model (model -> collection -> model) to be able to support these meta data attributes.
The text was updated successfully, but these errors were encountered: