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

Adding attributes to Collections? #1442

Closed
yanneves opened this issue Jun 22, 2012 · 3 comments
Closed

Adding attributes to Collections? #1442

yanneves opened this issue Jun 22, 2012 · 3 comments

Comments

@yanneves
Copy link

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 of song models:

var Album = Backbone.Collection.extend({
    model: song,
    properties: {
        name: "Greatest Hits",
        publisher: "Sony"
    }
});

the properties attribute could allow something like this:

Album.prop("name"); // "Greatest Hits"
Album.prop("publisher"); // "Sony"

Album.pluck("title"); // "Song1,Song2,Song3..."

Otherwise, at the moment we would have something like this, which relies on that collection being populated:

Album.first().get("albumName"); // "Greatest Hits"

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.

@braddunbar
Copy link
Collaborator

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!

@yanneves
Copy link
Author

@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!

@jaseemabid
Copy link

👍 @braddunbar , the answer helped :)

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

No branches or pull requests

3 participants