A super lightweight (½kb) JavaScript library with automatic method chaining, automatic context binding, event support and simple inheritance.
npm install fishbonec-{{{-<A super lightweight (½kb) JavaScript library – made for hungy pirates.
* clean and simple constructor
* scoped instance methods
* event observer pattern
* automatic method chaining
* no dependencies
* cross browser & Node.js support
* < 512 bytes minified (300 gzip)
* Model({ init: function(options){} }) - create a model
* new Model(options) - create an instance
* #on(event, listener) - observe a given event
* #off(event, [listener]) - remove listener
* #trigger(event, data) - triggers an event
``js
var Model = require("./fishbone");
var Pirate = Model({
init: function(name){
this.name = name;
this.grogs = 0;
},
drink: function(){
if (++this.grogs >= 100){
this.trigger("drunk");
}
},
sing: function(){
console.log("ARR: " + this.name + " WANT MORE!");
}
});
var captain = new Pirate("Jack"),
rounds = 20;
captain.on("drunk", captain.sing);
while (rounds--){
captain.drink().drink().drink().drink().drink();
}
`
Copy and paste this:
`js``
function Model(a){return function(b){var c=this,d={},e,f,g;c.on=function(a,b)
{var c=d[a]||(d[a]=[]);c.push(b)},c.trigger=function(a,b){for(var c=d[a],e=0;
c&&e
indexOf(b))>-1;)c.splice(e,1);d[a]=c};for(f in a)e=a[f],a.hasOwnProperty(f)&&
typeof e=="function"&&(c[f]=function(){var a=this.apply(c,arguments);return a
===g?c:a}.bind(e));c.init(b)}}typeof module=="object"&&(module.exports=Model);
Developed by Martin Kleppe at Ubilabs.
Released under the WTFPL license.