Class Table Of Contents

- All Classes
- All Files
- API
  - comb
    - logging
      - PropertyConfigurator
      - Level
      - BasicConfigurator
      - Logger
      - appenders
        - RollingFileAppender
        - ConsoleAppender
        - Appender
        - FileAppender
        - JSONAppender
    - PromiseList
    - Promise
    - regexp
    - array
    - string
    - collections
      - Pool
      - Iterable
      - HashTable
      - MaxHeap
      - Heap
      - Stack
      - AVLTree
      - AnderssonTree
      - Collection
      - RedBlackTree
      - Tree
      - Queue
      - BinaryTree
      - PriorityQueue
      - MinHeap
    - plugins
      - Middleware
      - Broadcaster
    - number
    - characters
    - date
- comb.plugins.Middleware
- Methods
  - _hook
  - post
  - post
  - pre
  - pre

# Class comb.plugins.Middleware

Plugin to enable middleware on a class
Defined in: Middleware.js.

Class Detail
comb.plugins.Middleware()

#### Example 1 :

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

    this.__hooks = obj.merge({}, this.__hooks);
    this._super(arguments);

Method Detail
{comb.Promise} _hook(state, op, args)

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

Parameters:
{"pre"|"post"} state
  the state in which the hook should be called
{String} op
  the operation that is being acted upong
args
  arguments to be passed into the listening functions.

Returns:
  {comb.Promise} a promise to use after middleware chain completes

    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() {

            if (count == length) {
                promise.callback();
            } else {

                var nextArgs = args.slice(0);
                nextArgs.unshift(next);
                funcs[count++].apply(this, nextArgs);
            }
        });
        next();
    } else {
        promise.callback();
    }
    return promise;

post(fun, callback)

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

NOTE:

- You must call next in order for the middleware chain to complete
- This connects to events on the instance of an object, NOT all instances!
- Hooks are called in the order they are received!
- When connecting your callback will be called in the scope of the classif you want a certain scope use comb.hitch

#### Example 1 :

    instance.post("save", function(next){
                   //do something...
                    //you have to call next!!!!!
                    next();
             });

Parameters:
fun
callback

    var hook = this.__hooks.post[fun];

    if (hook == undefined) {
        hook = this.__hooks.post[fun] = [];
    }
    hook.push(callback);

<static> comb.plugins.Middleware.post(name, cb)

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

NOTE:

- You must call next in order for the middleware chain to complete
- This connects to events on ALL instances of an object
- Hooks are called in the order they are received!
- When connecting your callback will be called in the scope of the classif you want a certain scope use comb.hitch

#### Example 1 :

    Class.post("save", function(next){
                  ...
                  //you must call next
             });

Parameters:
name
cb

    var hooks = this.prototype.__hooks;
    var hook = hooks.post[name];
    if (!hook) {
        hook = hooks.post[name] = [];
    }
    hook.push(cb);

<static> comb.plugins.Middleware.pre(name, cb)

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

NOTE:

- You must call next in order for the middleware chain to complete
- This connects to events on ALL instances of an object
- Hooks are called in the order they are received!
- When connecting your callback will be called in the scope of the classif you want a certain scope use comb.hitch

#### Example 1 :

    Class.pre("save", function(next){
                  ...
                  //you must call next
             });

Parameters:
name
cb

    var hooks = this.prototype.__hooks;
    var hook = hooks.pre[name];
    if (!hook) {
        hook = hooks.pre[name] = [];
    }
    hook.push(cb);

pre(fun, callback)
Use to listen to before an event occurred i.e. pre save NOTE:

- You must call next in order for the middleware chain to complete
- This connects to events on the instance of an object, not all instances!
- Hooks are called in the order they are received!
- When connecting your callback will be called in the scope of the classif you want a certain scope use comb.hitch

#### Example 1 :

         instance.pre("save", function(args,...., next){
             //do something...
             //you have to call next!!!!!
             next();
         });

Parameters:
fun
callback

    var hook = this.__hooks.pre[fun];
    if (!hook) {
        hook = this.__hooks.pre[fun] = [];
    }
    hook.push(callback);

Documentation generated by JsDoc Toolkit 2.4.0 on Tue Jan 31 2012 16:14:12 GMT-0600 (CST)