Skip to content

Instantly share code, notes, and snippets.

@rondorkerin
Last active August 29, 2015 13:57
Show Gist options
  • Save rondorkerin/9377821 to your computer and use it in GitHub Desktop.
Save rondorkerin/9377821 to your computer and use it in GitHub Desktop.
A Backbone.js view
var GoalView = (function(){
var my = {};
my.GoalCategory = Backbone.View.extend({
render: function(category) {
var data = this.model;
data['goals'] = this.collection.filter(function(model) {
return model.attributes['category'] == data.category;
});
var template = _.template($('#goalCategoryTpl').text(), data);
this.$el.html(template);
},
events: {
'keypress .new-goal': 'newGoal'
},
// associated functions
newGoal: function(event) {
if (event.which === 13) {
var newGoal = new GoalModel({
category: this.model.category,
goalText: $('.new-goal').val(),
userID: App.userID,
});
var self = this;
newGoal.save(null, {
success: function() {
self.collection.add(newGoal);
self.render();
},
error: function() {
console.log("Failed to save");
}
});
}
}
});
return my;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment