Skip to content
robrobbins edited this page Mar 2, 2014 · 2 revisions

The listener extension allows a View type Class to declare any/all of its event listeners via a hash (or collection of hashes) and have them be bound automagically. NOTE: All events bound with the listener extension are bound to the View's el. They can be delegated however. As with all extension objects you must extend your class with it, perhaps in the constructor of a subclass:

var Foo = function(el, data) {
  this.construct(el, data);
  $.extend(this, sudo.extensions.listener);
  this.bindEvents();
}

Notice the call to this.bindEvents in the above constructor. This is one of two methods exposed by the listener extension and the one you must call before your event binding will take place. Though the above call is placed in the constructor in can happen from anywhere at anytime.

###bindEvents

Expects that this object has an 'event' hash or an 'events' array (of hashes) in its data hash (this.data). The format of each 'event hash' should be:

{
  on: 'eventName',
  sel: 'someSelector', // optional
  data: {foo: 'bar'}, // optional
  fn: 'someFunction'
}

####on

A DOM compatible event name.

####sel

If present will instruct Cash to delegate this event to the given 'selector'.

####data

This will be directly placed into the Event passed to 'someFunction', available like so:

someFunction: function(event) {
  console.log(event.data.foo); // => 'bar'
}

Very useful in situation where you want to bind an event handler to a View object's send function (for example) and you need to specify a sendMethod:

{
  on: 'click',
  sel: 'btn.foo',
  data: {sendMethod: 'handleFooClick'},
  fn: 'send'
}

This would instruct the View class to invoke it's send method, passing itself to a method named 'handleFooClick' on its parent (or parent's parent etc...).

####fn

Passed as a string, a method by this name is expected to exist on this. When found it will be bound (scope is this object) as the event handler for the given event on.

###unbindEvents

Instruct cash to unbind the event(s) located in this Object's data hash.

Clone this wiki locally