-
-
Save ravidsrk/1998653 to your computer and use it in GitHub Desktop.
Rewriting Peep Code app.coffee example
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
# CoffeeScript With Mixins | |
$ -> | |
template = _.templateFor '#meal-template' | |
meal = new Meal | |
_.focusOn '#entry' | |
_.onSumbitOf '#entry_form', -> | |
meal.add new Dish _.valOf '#entry' | |
_.setHtmlOf 'ul#meal', template meal.toJSON() | |
_.resetValOf '#entry' | |
# Using these generic helper functions added to Underscore.js | |
_.mixin | |
valOf: (sel) -> $(sel).val() | |
setValOf: (sel, val) -> $(sel).val val | |
resetValOf: (sel) -> $(sel).val '' | |
templateFor: (sel) -> _.template $(sel).html() | |
htmlOf: (sel) -> $(sel).html() | |
setHtmlOf: (sel, html) -> $(sel).html html | |
focusOn: (sel) -> $(sel).focus() | |
onSumbitOf: (sel, fn) -> $(sel).submit (event) -> | |
event.preventDefault() | |
fn() | |
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
#Plain CoffeeScript | |
$ -> | |
template = _.template ($ '#meal-template').html() | |
meal = new Meal | |
($ '#entry').focus() | |
($ '#entry_form').submit (event) => | |
event.preventDefault() | |
meal.add new Dish ($ '#entry').val() | |
($ 'ul#meal').html template meal.toJSON() | |
($ '#entry').val('') |
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
//Using JavaScript | |
$(function(){ | |
var template = _.template($('#meal-template').html()); | |
var meal = new Meal(); | |
$('#entry').focus(); | |
$('#entry_form').submit(function(event) { | |
event.preventDefault(); | |
meal.add(new Dish($('#entry').val())); | |
$('ul#meal').html(template(meal.toJSON())); | |
$('#entry').val(''); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment