Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

Observable Api

observable(el)

Adds Observer support for the given object el or if the argument is empty a new observable instance is created and returned. After this the object is able to trigger and listen to events. For example:

function Car() {

  // Make Car instances observable
  observable(this)

  // listen to 'start' event
  this.on('start', function() {
    // engine started
  })

}

// make a new Car instance
var car = new Car()

// trigger 'start' event
car.trigger('start')

@returns the given object el or a new observable instance

el.on(events, callback)

Listen to the given space separated list of events and execute the callback each time an event is triggered.

// listen to single event
el.on('start', function() {

})

// listen to multiple events, the event type is given as the argument
el.on('start stop', function(type) {

  // type is either 'start' or 'stop'

})

// listen all the events of this observable
el.on('*', function(event, param1, param2) {
  // event will be the name of any event triggered
  // do something with the parameters
})

@returns el

el.one(event, callback)

Listen to the given space separated list of events and execute the callback at most once

// run the function once, even if 'start' is triggered multiple times
el.one('start', function() {

})

@returns el

el.off(events)

Removes the given space separated list of events listeners.

el.off('start stop')

@returns el

el.off(events, fn)

Removes the given callback from the list of events

function doIt() {
  console.log('starting or ending')
}

el.on('start middle end', doIt)

// remove a specific listener from start and end events
el.off('start end', doIt)

@returns el

el.off('*')

Removes all listeners from all event types.

@returns el

el.off('*', fn)

Removes the specific callback function called on all the events

@returns el

el.trigger(events)

Execute all callback functions that listen to the given space separated list of events.

el.trigger('start')
el.trigger('render update')

@returns el

el.trigger(event, arg1 ... argN)

Execute all callback functions that listen to the given event. Any number of extra parameters can be provided for the listeners.

// listen to 'start' event and expect extra arguments
el.on('start', function(engine_details, is_rainy_day) {

})

// trigger start event with extra parameters
el.trigger('start', { fuel: 89 }, true)

@returns el

namespacing

Events can be namespaced on a single level by using a . delimiter. Namespaced events listen to the primary event and can also be specifically triggered or removed.

// listen to start and start.honda events
el.on('start.honda', function() {
})

// trigger all start events (including start.honda)
el.trigger('start')

// trigger only start.honda events
el.trigger('start.honda')

// remove only honda start events
el.off('start.honda')

// remove all start events (including start.honda)
el.off('start')