Last active
December 11, 2015 16:08
-
-
Save shairontoledo/4625274 to your computer and use it in GitHub Desktop.
Port from http://net.tutsplus.com/tutorials/javascript-ajax/build-a-contacts-manager-using-backbone-js-part-1/
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
$ -> | |
contacts = [ | |
{ name: "Contact 1", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" } | |
{ name: "Contact 2", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" } | |
{ name: "Contact 3", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" } | |
{ name: "Contact 4", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" } | |
{ name: "Contact 5", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" } | |
{ name: "Contact 6", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" } | |
{ name: "Contact 7", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" } | |
{ name: "Contact 8", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" } | |
] | |
class Contact extends Backbone.Model | |
defaults: | |
photo: "img/placeholder.png" | |
class Directory extends Backbone.Collection | |
model: Contact | |
class ContactView extends Backbone.View | |
tagName: "article", | |
className: "contact-container", | |
template: $("#contactTemplate").html() | |
render: -> | |
tmpl = _.template @template | |
$(@el).html tmpl(@model.toJSON()) | |
this | |
class DirectoryView extends Backbone.View | |
el: $("#contacts") | |
initialize: -> | |
@collection = new Directory(contacts) | |
@render() | |
render: -> @renderContact(item) for item in @collection.models | |
renderContact: (item) -> | |
contactView = new ContactView model: item | |
@$el.append(contactView.render().el) | |
window.directory = new DirectoryView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment