Can you set attributes on Backbone.js Collections? If so, how?

backbone.js

Solution

Yes, it's possible. Look at the signature of the `Collection` constructor:

new Collection([models], [options])

So you could write like this:

var ListItem = Backbone.Model.extend({});
var TodoList = Backbone.Collection.extend({
    model: ListItem,
    initialize: function(models, options) {
        options || (options = {});
        if (options.title) {
            this.title = options.title;
        };
    }
})

var iPromise = new TodoList([], {
    title: 'NY Resolutions'
})

console.log(iPromise.title);

Problem

I am just getting into backbone.js and I thought the best way to get into it is to actually build a todo list (yea...pretty original). Humor aside, I have searched google and the docs and stackoverflow of course, for a way to add an attribute to a collection. So, in my case a todo list is a collection of listitems. However a Todo List can have a title as according to my design, I want to be able to create multiple lists. ``` var TodoList = Backbone.Collection.extend({ model: ListItem }); //is this possible for collections? var newTodoList = new TodoList({name: "My first list"}); ``` Thanks a lot for the help! Appreciate it!

Original source