Fishbone.js c-{{{-<
A super lightweight (½kb) JavaScript library with automatic method chaining, automatic context binding, event support and simple inheritance.
Version: 1.0.1
Features
- clean and simple syntax
- steep learning curve
this
is the context in all methods- automatic method chaining
- simple inheritance
on
, off
event observer pattern- no dependencies
- production ready
- well tested
- cross browser & Node.js support
- ~ 512 bytes minified (300 gzipped !)
Usage
Klass = Model({ init: function(options){} })
- create a modelKlass2 = Klass.extend({ … })
- inherit from other classinstance = new Klass(options)
- create an instanceinstance.on(event, listener)
- observe a given eventinstance.off(event, [listener])
- remove listenerinstance.trigger(event, data)
- triggers an event
Example
var Model = require("./fishbone");
var Pirate = Model({
likes: "GROG",
init: function(name){
this.name = name;
this.grogs = 0;
},
drink: function(){
if (++this.grogs >= 100){
this.trigger("drunk");
}
},
yell: function(){
console.log("WANT MORE: " + this.likes);
}
});
var Captain = Pirate.extend({
likes: "STRONG GROG"
});
var captain = new Captain("Jack"),
rounds = 20;
captain.on("drunk", captain.yell);
function neverbeingcalled(){
console.error("AAR!");
}
captain.on("drunk", neverbeingcalled);
captain.off("drunk", neverbeingcalled);
while (rounds--){
captain
.drink()
.drink()
.drink()
.drink()
.drink();
}
Install
Include fishbone.js
or copy and paste this:
Model=function a(b,c,d,e){function f(){var a=this,f={};a.on=function(a,b){(f[a]||
(f[a]=[])).push(b)},a.trigger=function(a,b){for(var c=f[a],d=0;c&&d<c.length;)c
[d++](b)},a.off=function(a,b){for(d=f[a]||[];b&&(c=d.indexOf(b))>-1;)d.splice(c
,1);f[a]=b?d:[]};for(c in b)d=b[c],a[c]=typeof d=="function"?function(){return(
d=this.apply(a,arguments))===e?a:d}.bind(d):d;a.init&&a.init.apply(a,arguments)
}return f.extend=function(f){d={};for(c in b)d[c]=b[c];for(c in f)d[c]=f[c],b[c
]!==e&&(d["__"+c]=b[c]);return a(d)},f},typeof module=="object"&&(module.exports
=Model);
Install via npm:
npm install fishbone
Browser Support
Fishbone runs in all modern browsers and Node.js.
If you want support for Internet Explorer 8 and below, you have to include the
.bind
and .indexOf
polyfills: https://github.com/aemkei/fishbone.js/blob/master/polyfills.js
About
Developed by Martin Kleppe at Ubilabs.
Released under the WTFPL license.