diff --git a/chapters/jquery/callback-bindings-jquery.textile b/chapters/jquery/callback-bindings-jquery.textile new file mode 100644 index 0000000..c656dc3 --- /dev/null +++ b/chapters/jquery/callback-bindings-jquery.textile @@ -0,0 +1,32 @@ +--- +layout: recipe +title: Callback bindings # using => instead of -> +chapter: jQuery +--- + +h2. Problem + +You want to bind a callback function to an object. + +h2. Solution + +{% highlight coffeescript %} +$ -> + class Basket + constructor: () -> + @products = [] + + $('.product').click (event) => + @add $(event.target).attr 'id' + + add: (product) -> + @products.push product + console.log @products + + new Basket() +{% endhighlight %} + +h2. Discussion + +By using the fat arrow => instead of the normal arrow -> the function gets +automatically bound to the object and can access the @-variable