Skip to content
This repository
Browse code

Additional docs and info

corrected some sample code, and added more info about the benefits of EventBinder use.
  • Loading branch information...
commit 08eea2945ec0ef47984cf8aaa3583a773a542381 1 parent 11b94c8
Derick Bailey derickbailey authored

Showing 1 changed file with 20 additions and 4 deletions. Show diff stats Hide diff stats

  1. +20 4 readme.md
24 readme.md
Source Rendered
@@ -38,22 +38,38 @@ events, across any number of objects that trigger the events. This allows
38 38 events to be grouped together and unbound with a single call during the
39 39 clean-up of an object that is bound to the events.
40 40
  41 +Ultimately, the EventBinder calls back to the standard Backbone `on` method
  42 +of the object for which events are being handled. The benefit of using the
  43 +EventBinder then, is that you no longer have to manually manage calling `off`
  44 +for each of these events, and you can safely use anonymous callback functions
  45 +as event arguments and stil be able to unbind them when needed.
  46 +
41 47 ### Bind Events
42 48
  49 +The basic syntax for binding events is to use the `bindTo` method which
  50 +follows the path of Backbone's `on` method for events, but adds one parameter
  51 +to the beginning of the method call: the object that triggers the event.
  52 +
  53 +For example, if you have a model that you want to listen for events from,
  54 +you can use the EventBinder to manage the event for you:
  55 +
43 56 ```js
44 57 var binder = new Backbone.EventBinder();
45 58
46 59 var model = new MyModel();
47 60
48 61 var handler = {
49   - doIt: function(){}
  62 + doIt: function(){ /* ... */ }
50 63 }
51 64
52   -binder.bindTo(model, "change:foo", handler.doIt);
  65 +// same args list as model.on, but putting the model as the first parameter
  66 +binder.bindTo(model, "change:foo", handler.doIt, handler);
53 67 ```
54 68
55   -You can optionally specify a 4th parameter as the context in which the callback
56   -method for the event will be executed:
  69 +You can specify a 4th parameter as the context in which the callback
  70 +method for the event will be executed. If you leave the empty, the default
  71 +context will be used (varies depending on other circumstances) just like
  72 +Backbone's events.
57 73
58 74 ```js
59 75 binder.bindTo(model, "change:foo", someCallback, someContext);

0 comments on commit 08eea29

Please sign in to comment.
Something went wrong with that request. Please try again.