Skip to content

reergymerej/rednib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rednib v0.0.2 Build Status

rednib takes any object and makes it observable.

var pojo = {};
rednib(pojo);

var onFoo = function () { console.log('onFoo'); };

pojo.bind('foo', onFoo);
pojo.trigger('foo', onFoo);  // logs 'onFoo'
pojo.unbind('bar', onBar);  // omit the handler to remove all 'bar' handlers

The following will be added to your observable objects:

  • bind (Function)
  • unbind (Function)
  • trigger (Function)
  • _handlers (Array)

If you would prefer different function names, you can alias them.

rednib.alias('bind', 'when');
rednib.alias('trigger', 'broadcast');
rednib.alias('unbind', 'forget');

pojo.when('foo', onFoo);
pojo.broadcast('foo', onFoo);  // logs 'onFoo'
pojo.forget('foo', onFoo);

================================================

More Info

You an assign multiple handlers at once with an object.

pojo.bind({
  foo: function () {},
  bar: function () {},
  baz: function () {}
});

rednib, bind, trigger, and unbind all return the observable object, so they can be chained.

rednib({}).
  bind('foo', function () { console.log('yo'); }).
  trigger('foo').
  unbind('foo');

alias returns the rednib object, so you can chain alias calls.

rednib.alias('bind', 'when').alias('trigger', 'broadcast');

// To demonstrate, you can do the following, though I don't know why you would.
rednib.alias('bind', 'whenISay')({}).whenISay('hey', function () { console.log('ho!')}).trigger('hey').unbind('hey');

You can include data when triggering an event.

obj.on('foo', function (data) {
  // do something with data
});

obj.trigger('foo', { bar: true, baz: false });

================================================

Please create an issue for feature requests or to report bugs.

Coming Soon

  • once
  • handler scope