Plugin to enable middleware on a class
Examplevar Mammal = define(comb.plugins.Middleware, { instance : { constructor: function(options) { options = options || {}; this.super(arguments); this._type = options.type || "mammal"; }, speak : function() { var ret = new comb.Promise(); this._hook("pre", "speak") .then(comb.hitch(this, "_hook", "post", "speak"), hitch(ret, "errback")) .then(comb.hitch(ret, "callback"), comb.hitch(ret, "errback")); return ret; } } }); Mammal.pre('speak', function(next){ //do something meaningful next(); }); var m = new Mammal({color : "gold"}); m.speak();
function (){ this.__hooks = obj.merge({}, this.__hooks); this._super(arguments); }
Use to listen to after an event has occurred i.e. post save
NOTE:
Class.post("save", function(next){ ... //you must call next });Arguments
function (name,cb){ var hooks = this.prototype.__hooks; var hook = hooks.post[name]; if (!hook) { hook = hooks.post[name] = []; } hook.push(cb); }
Use to listen to after an event has occurred i.e. post save
NOTE:
Class.pre("save", function(next){ ... //you must call next });Arguments
function (name,cb){ var hooks = this.prototype.__hooks; var hook = hooks.pre[name]; if (!hook) { hook = hooks.pre[name] = []; } hook.push(cb); }
Protected!
Call to initiate middleware for the topic
NOTE: this function takes a variable number of arguments whatever comes after the op param will be passed into the listening function, with the last argument to the listenting function being the next function
Argumentsthe state in which the hook should be called
the operation that is being acted upong
arguments to be passed into the listening functions.
comb.Promise
a promise to use after middleware chain completes
function (state,op,args){ args = args || []; var promise = new Promise(); var funcs, length; if (this.__hooks[state] && (funcs = this.__hooks[state][op]) != null && (length = funcs.length) > 0) { var count = 0; var next = func.hitch(this, function (err) { if (err) { promise.errback(err); } else { nextTick(func.hitch(this, function () { //if Ive looped through all of them callback if (count === length) { promise.callback(); } else { //call next var nextArgs = args.slice(0); nextArgs.unshift(next); funcs[count++].apply(this, nextArgs); } })); } }); next(); } else { promise.callback(); } return promise.promise(); }
Use to listen to after an event has occurred i.e. post save
NOTE:instance.post("save", function(next){ //do something... //you have to call next!!!!! next(); });Arguments
function (fun,callback){ var hook = this.__hooks.post[fun]; //if I havent initialized it create it; if (hook === undefined) { hook = this.__hooks.post[fun] = []; } hook.push(callback); }
Use to listen to before an event occurred i.e. pre save
NOTE:
instance.pre("save", function(args,...., next){ //do something... //you have to call next!!!!! next(); });Arguments
function (fun,callback){ var hook = this.__hooks.pre[fun]; if (!hook) { hook = this.__hooks.pre[fun] = []; } hook.push(callback); }
MIT https://github.com/C2FO/comb/raw/master/LICENSE
git clone git://github.com/C2FO/comb.git