fishbone

A super lightweight (½kb) JavaScript library with automatic method chaining, automatic context binding, event support and simple inheritance.

Fishbone.js c-{{{-<

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
var Model = require("./fishbone");
 
var Pirate = Model({
  initfunction(name){
    this.name = name;
    this.grogs = 0;
  },
  drinkfunction(){
    if (++this.grogs >= 100){
      this.trigger("drunk");
    }
  },
  singfunction(){
    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:

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<c.length;e++)c[e](b)},c.off=function(a,b){for(var c=d[a]||[],e;b&&(e=c.
indexOf(b))>-1;)c.splice(e,1);d[a]=c};for(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.