-
-
Save nhunzaker/d173a122b55a9486113c to your computer and use it in GitHub Desktop.
Refactoring to Backbone Final Result
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
Handlebars.registerHelper('prettyDate', function(dateString) { | |
var date = new Date(dateString); | |
return [ date.getMonth(), date.getDate(), date.getFullYear() ].join("/") | |
}); |
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
var NewsCollection = Backbone.Collection.extend({ | |
model: NewsModel, | |
url: '/my/api/news' | |
}); |
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
var NewsModel = Backbone.Model.extend({ | |
urlRoot: '/my/api/news' | |
}); |
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
//= require templates/_news_item | |
//= require templates/news_list | |
var NewsView = Backbone.View.extend({ | |
tagName: 'ul', | |
className: 'news_widget', | |
template: HandlebarsTemplates['news_list'], | |
itemTemplate: HandlebarsTemplates['_news_item'], | |
initialize: function(options) { | |
this.listenTo(this.collection, { | |
'reset' : this.render, | |
'add' : this.addEntry | |
}); | |
}, | |
render: function() { | |
var markup = this.template({ items: this.collection.toJSON() }); | |
this.$el.html(markup); | |
}, | |
addEntry: function(model) { | |
var markup = this.itemTemplate(model.toJSON()); | |
this.$el.append(markup); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment