Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adapt application to deal with persisted data
- Loading branch information
Showing
3 changed files
with
30 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,53 @@ | ||
ContactManager.module("Entities", function(Entities, ContactManager, Backbone, Marionette, $, _){ | ||
Entities.Contact = Backbone.Model.extend({}); | ||
Entities.Contact = Backbone.Model.extend({ | ||
urlRoot: "contacts" | ||
}); | ||
|
||
Entities.configureStorage(Entities.Contact); | ||
|
||
Entities.ContactCollection = Backbone.Collection.extend({ | ||
url: "contacts", | ||
model: Entities.Contact, | ||
comparator: "firstName" | ||
}); | ||
|
||
var contacts; | ||
Entities.configureStorage(Entities.ContactCollection); | ||
|
||
var initializeContacts = function(){ | ||
contacts = new Entities.ContactCollection([ | ||
{ id: 1, firstName: "Alice", lastName: "Arten", phoneNumber: "555-0184" }, | ||
{ id: 2, firstName: "Bob", lastName: "Brigham", phoneNumber: "555-0163" }, | ||
{ id: 3, firstName: "Charlie", lastName: "Campbell", phoneNumber: "555-0129" } | ||
]); | ||
contacts.forEach(function(contact){ | ||
contact.save(); | ||
}); | ||
return contacts; | ||
}; | ||
|
||
var API = { | ||
getContactEntities: function(){ | ||
if(contacts === undefined){ | ||
initializeContacts(); | ||
var contacts = new Entities.ContactCollection(); | ||
contacts.fetch(); | ||
if(contacts.length === 0){ | ||
// if we don't have any contacts yet, create some for convenience | ||
return initializeContacts(); | ||
} | ||
return contacts; | ||
}, | ||
|
||
getContactEntity: function(contactId){ | ||
var contact = new Entities.Contact({id: contactId}); | ||
contact.fetch(); | ||
return contact; | ||
} | ||
}; | ||
|
||
ContactManager.reqres.setHandler("contact:entities", function(){ | ||
return API.getContactEntities(); | ||
}); | ||
|
||
ContactManager.reqres.setHandler("contact:entity", function(id){ | ||
return API.getContactEntity(id); | ||
}); | ||
}); |