Sunny Ho Sunny Ho - 1 month ago 21
jQuery Question

backbone confusion on collections and model

There's a folder of js/collections/contact.js and it has only

ContactManager.Collections.Contacts = Backbone.Collection.extend({
model: ContactManager.Models.Contact
});


The demo of a small app : http://dmytroyarmak.github.io/backbone-contact-manager/#contacts

I'm trying to guess what does it do, but have no clue so far.

Answer

It only creates a new collection type and demonstrate how to encapsulate each component of an app. This project uses the global object ContactManager as a kind of namespace for the app.

The collection is used here:

var contacts = new ContactManager.Collections.Contacts(data.contacts),

And is equivalent to:

var contacts = new Backbone.Collection(data.contacts, {
    model: ContactManager.Models.Contact
});

Which mean each object inside data.contacts is made into a ContactManager.Models.Contact model object.

Additional documentation: