chain.js
is designed to make chaining-based APIs easy.
Usage
Here's an example:
var Ajax = Chain; Ajax;Ajax; Ajax; var request = Ajaxgetjson; ; request
Mutating Methods
Chain.def( fn )
Chain.getter( fn )
Chain.lazy( fn )
Chain.flag( name, properties )
Cloning Methods
Chain.with()
Chain.clone
Chain.tap()
Chain.promise()
Chain.then()
Chain.catch()
#def( namedFunction )
def
is used to assign non-enumerable functions.
Values can be assigned with #def( name, value )
.
#getter( namedFunction )
Similar to def
except getters are called without parenthesis:
var Invoice = Chainclone; Invoice; var invoice = Invoice invoiceisDue === false;
#lazy( namedFunction )
lazy
is just like getter
, but it re-assigns itself
as the return value of the passed function.
#with( attributes )
Create a clone with the passed attributes appended:
var person = Chain;
#flag( name, attributes )
flag
is a shortcut for returning a clone with attributes appended:
var Order = Chainclone; Order;Order; var order1 = OrderasShipped;var order2 = order1asDelivered; order1status === 'shipped';order2status === 'delivered';
#clone
#tap( fn )
tap
creates a clone, applies fn
to it, and then returns it.
The clone is also passed as the first argument.
Chain;
#promise( fn )
chain.js
promises require an ES6-compatible window.Promise
object.
Alternatively, you can set your own: Chain.def('Promise', RSVP.Promise)
.
chain.js
promises are lazy: fn
isn't invoked until #then()
has been called on the chain.