primal /ˈprʌɪm(ə)l/
most important; primary or fundamental.
primal.js is a small utility library heavily inspired by riot.js and Mootools. Why primal.js when there is riot.js? The short explanation is that I prefer having the choice of extending the prototype. In addition, primal.js does not have any dependencies, and will never have, on external libraries for instance jQuery.
There are two ways to use primal, first by extending the prototype:
var Car = function(){
primal.observable(this, Car);
};
Or by extending on instance of an object:
var car = primal.observable({});
Handle events on native javascript object by extending them with primal.observable
- instance (object)- The object to extend
- class* (constructor) - Function to extend prototype on
*optional
Example:
var car = primal.observable({});
Add an event listener to an object.
- event (string) - The event to listen to
- fn (function) - The function to call when the event is triggered
Example:
car.addEvent('running', function(){...});
Add several events listeners to an object.
- options (object) - Object defining the events to listen to
Example:
car.addEvents({
running: function(){...},
stoped: function(){...}
});
Fire an event. Events can be triggered both from the object or from the outside.
- event (string) - The event to trigger
- arg|[arg] (mixed|array) - An argument or an array of arguments
- delay (integer) - The delay before triggering the event
Example:
Car.prototype.start = function(){
this.isStarted = true;
this.fireEvent('running', [this.timesStarted, this.driver]);
};
Remove an event listener from an object.
- event (string) - The event to remove from
- fn (function) - The function to remove
Example:
var eventListner = function(timeStarted, driver){...};
car.addEvent('running', eventListner);
...
car.removeEvent('running', eventListner);
Remove all events of the specified type
- event (string) - The event to remove
Example:
car.removeEvent('running');
Subscribe and publish messages.
- instance (object) - The object to extend
- class* (optional) - Function to extend prototype on
Example:
mediator = primal.mediator({});
Subscribe to topic on the event mediator.
- topic (string) - The event to remove from
- fn (function) - The function to remove
Example:
var eventListner = function(){...};
car.subscribe('keyTurned', eventListner);
Publish an message to the event mediator.
- topic (string) - The topic to publish
- arg|[arg] (mixed|array) - An argument or an array of arguments
- delay (integer) - The delay before triggering the event
Example:
car.publish('keyTurned', []);
Unsubscibe, stop listening, to a topic on the event mediator.
- topic (string) - The event to remove from
- fn (function) - The function to remove
Example:
var eventListner = function(){...};
car.subscribe('keyTurned', eventListner);
...
car.unsubscribe('keyTurned', eventListner);
primal.js is licensed under the MIT license