Moving Contacts to the Entities Module
Learn how to create a separate file for our contact management in Marionette and how to request the contacts from the file.
Moving the contact entities
Now that we have a better understanding of modules, let’s move our contacts from index.html into the contacts module:
ContactManager.module("Entities", function(Entities, ContactManager,Backbone, Marionette, $, _){Entities.Contact = Backbone.Model.extend({});Entities.ContactCollection = Backbone.Collection.extend({model: Entities.Contact,comparator: "firstName"});});
Note: We’ve attached our model and collection as attributes to the module, and not the application, as was the case before. In other words, we’ve gone from using
ContactManager.Contactto access contacts to usingEntities.Contactfor accessing them. Since we’re attaching them to the module, they will be publicly accessible. This means we can access them from elsewhere in the application, if necessary.
Since we’ve changed where our contact definitions are, we need to adapt our index.html file:
var contacts = new ContactManager.Entities.ContactCollection([// our contact data goes here]);
Note we’ve simply changed: ContactManager.ContactCollection to ContactManager.Entities.ContactCollection, since the ContactCollection is now attached to the Entities module and no longer directly attached to the ContactManager app. ...