Plugin to enable middleware on a class

Example
var 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();
            

Constructor

Defined plugins/Middleware.js Source
function (){
   this.__hooks = obj.merge({}, this.__hooks);
   this._super(arguments);
           
}
            

post Static Function Public


Defined plugins/Middleware.js

Use to listen to after an event has occurred i.e. post save

NOTE:

Example
Class.post("save", function(next){
              ...
              //you must call next
         });
        
Arguments Source
function (name,cb){
   var hooks = this.prototype.__hooks;
   var hook = hooks.post[name];
   if (!hook) {
       hook = hooks.post[name] = [];
   }
   hook.push(cb);
           
}
    

pre Static Function Public


Defined plugins/Middleware.js

Use to listen to after an event has occurred i.e. post save

NOTE:

Example
Class.pre("save", function(next){
              ...
              //you must call next
         });
        
Arguments Source
function (name,cb){
   var hooks = this.prototype.__hooks;
   var hook = hooks.pre[name];
   if (!hook) {
       hook = hooks.pre[name] = [];
   }
   hook.push(cb);
           
}
    

_hook Function Public


Defined plugins/Middleware.js

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

Arguments Returns Source
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();
           
}
    

post Function Public


Defined plugins/Middleware.js

Use to listen to after an event has occurred i.e. post save

NOTE:

Example
instance.post("save", function(next){
               //do something...
                //you have to call next!!!!!
                next();
         });
        
Arguments Source
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);
           
}
    

pre Function Public


Defined plugins/Middleware.js

Use to listen to before an event occurred i.e. pre save

NOTE:

Example
instance.pre("save", function(args,...., next){
         //do something...
         //you have to call next!!!!!
         next();
     });
        
Arguments Source
function (fun,callback){
   var hook = this.__hooks.pre[fun];
   if (!hook) {
       hook = this.__hooks.pre[fun] = [];
   }
   hook.push(callback);
           
}
    

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta