easierbycode
3/9/2012 - 2:56 PM

jQuery callback bindings

jQuery callback bindings

# http://coffeescriptcookbook.com/chapters/jquery/callback-bindings-jquery

# By using the fat arrow (=>) instead of the normal arrow (->) the function gets automatically bound to the object and can access the @-variable.

$ ->
  class Basket
    constructor: () ->
      @products = []

      $('.product').click (event) =>
        @add $(event.currentTarget).attr 'id'

    add: (product) ->
      @products.push product
      console.log @products

  new Basket()