Skip to content

Instantly share code, notes, and snippets.

@mythz
Created May 20, 2011 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mythz/982229 to your computer and use it in GitHub Desktop.
Save mythz/982229 to your computer and use it in GitHub Desktop.
Rewriting Peep Code app.coffee example
# 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()
#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('')
//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