Class comb.plugins.Middleware
Plugin to enable middleware on a class
Defined in: Middleware.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Method Attributes | Method Name and Description |
---|---|
_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 |
|
post(fun, callback)
Use to listen to after an event has occurred i.e. |
|
<static> |
comb.plugins.Middleware.post(name, cb)
Use to listen to after an event has occurred i.e. |
<static> |
comb.plugins.Middleware.pre(name, cb)
Use to listen to after an event has occurred i.e. |
pre(fun, callback)
Use to listen to before an event occurred i.e.
|
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();
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
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
instance.post("save", function(next){ //do something... //you have to call next!!!!! next(); });
- Parameters:
- 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 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
Class.post("save", function(next){ ... //you must call next });
- Parameters:
- 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
Class.pre("save", function(next){ ... //you must call next });
- Parameters:
- name
- cb
- 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
instance.pre("save", function(args,...., next){ //do something... //you have to call next!!!!! next(); });
- Parameters:
- fun
- callback