• Stars
    star
    399
  • Rank 108,092 (Top 3 %)
  • Language
    JavaScript
  • License
    Do What The F*ck ...
  • Created about 12 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Fishbone.js c-{{{-<

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

NPM version

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 model
  • Klass2 = Klass.extend({ … }) - inherit from other class
  • instance = new Klass(options) - create an instance
  • instance.on(event, listener) - observe a given event
  • instance.off(event, [listener]) - remove listener
  • instance.trigger(event, data) - triggers an event

Example

var Model = require("./fishbone");              // require module (Node only)

var Pirate = Model({                            // simple class creator
  likes: "GROG",                                // instance properties
  init: function(name){                         // init is the main entrance
    this.name = name;                           // options can be passed
    this.grogs = 0;
  },
  drink: function(){                            // instance method
    if (++this.grogs >= 100){
      this.trigger("drunk");                    // trigger an event
    }
  },
  yell: function(){
    console.log("WANT MORE: " + this.likes);    // this is always in context
  }
});

var Captain = Pirate.extend({                   // simple inheritance
  likes: "STRONG GROG",                         // override property
  yell: function(){                             // override method
    console.log("Avast, me hearties!");
    this.__yell();                              // call super method
  }
});

var captain = new Captain("Jack"),              // create an instance
  rounds = 20;

captain.on("drunk", captain.yell);              // add event listener

function neverbeingcalled(){                    // pseudo listener
  console.error("AAR!");
}

captain.on("drunk", neverbeingcalled);          // add event listener
captain.off("drunk", neverbeingcalled);         // remove event listener

while (rounds--){
  captain
    .drink()                                    // chaining FTW!
    .drink()                                    // chaining FTW!
    .drink()                                    // chaining FTW!
    .drink()                                    // chaining FTW!
    .drink();                                   // chaining FTW!
}

Install

Include fishbone.min.js or copy and paste this:

Model=function a(b,c,d,e){function f(){var a=this,f={};a.on=function(b,c){return(f[b]||
(f[b]=[])).push(c),a},a.trigger=function(a,b){for(var c=f[a],d=0;c&&d<c.length;)c[d++](
b)},a.off=function(b,e){for(d=f[b]||[];e&&(c=d.indexOf(e))>-1;)d.splice(c,1);return f[b]
=e?d:[],a};for(c in b)d=b[c],a[c]="function"==typeof d?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},"object"==typeof module&&(module.exports=Model);                         // c-{{{-<

Install via npm:

npm install fishbone

Install via bower:

bower install --save 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.

Analytics

More Repositories

1

jsfuck

Write any JavaScript with 6 Characters: []()!+
JavaScript
7,745
star
2

tixy

JavaScript
186
star
3

world

An Animated Globe in 1024 Bytes of JavaScript
JavaScript
172
star
4

katakana.js

Write JavaScript alert(1) with Katakana characters only
JavaScript
135
star
5

aurebesh.js

Translate JavaScript to Other Alphabets
CSS
46
star
6

ascii.js

Convert Image to ASCII Using HTML5 Canvas
JavaScript
31
star
7

aemkei.github.io

HTML
23
star
8

bash

Mac OS X Bash Extensions
Shell
15
star
9

tic-tac-tweet

An HTML and JavaScript Version of Tic Tac Toe in a Tweet (140 Characters)
HTML
11
star
10

symmetry

Symmetric JavaScript
JavaScript
10
star
11

incept10n

HTML
8
star
12

codejam.js

Google Code Jam JavaScript Template
JavaScript
7
star
13

sic

HTML
6
star
14

five

HTML
5
star
15

jstemplate

SVN Copy of Google Code: Client-side Templating System
JavaScript
5
star
16

oo

Write JavaScript with Only One Letter
HTML
5
star
17

jslint-tmbundle

JavaScript Lint Textmate Bundle
JavaScript
5
star
18

oso_examples

Sample OpenSocial Applications
5
star
19

pyte

JavaScript Dependency Management and Deployment Framework
JavaScript
3
star
20

theseus

Creating and solving a maze in only 256 bytes of JavaScript.
HTML
3
star
21

poker_js1k

A Poker Game in exactly 1024 Bytes
JavaScript
3
star
22

backfish.js

A Fishbone.js drop-in replacement for Backbone.js
2
star
23

JSC-Prototype

Prototype.js port to work with JSC. Lets you run JavaScript code from the Mac OS X command line.
JavaScript
2
star
24

thunderbolt

A Firebug Logger Extension for Flash ActionScript 2
2
star
25

leap

Entry for ONESECOND – The Leap Second Festival
HTML
1
star
26

as

CSS
1
star
27

simulateGeolocation.js

Simulate a random movement behavior of the Geolocation API
JavaScript
1
star
28

mono

JavaScript
1
star
29

octagram

HTML
1
star