Last active
August 29, 2015 13:57
-
-
Save rondorkerin/9377821 to your computer and use it in GitHub Desktop.
A Backbone.js view
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 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