Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions chapters/jquery/callback-bindings-jquery.textile
Original file line number Diff line number Diff line change
@@ -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