Comb-proxy is plugin for comb to expose ProxyHelpers.
npm install comb-proxy
To initialize comb-proxy simply require it.
var comb = require("comb-proxy");
##Namespaces
##Classes
Alias for comb.
###createFunctionWrapper static function public
Defined proxy.js Top
Creates a function proxy for an object.
Example
//create an object that can use properties or as a function through the new operator
var MyObject = comb.define(null, {
instance : {
hello : "hello",
constructor : function(){
this.args = comb.argsToArray(arguments);
}
}
});
//NOTE: this will not work properly for native objects like Date.
var createNewMyObject = function(){
try {
p = new MyObject();
} catch (ignore) {
//ignore the error because its probably from missing arguments
}
//Now lets take care of arguments supplied!!!
MyObject.apply(p, comb.argsToArray(arguments));
return p;
};
//This example creates an object with a world property but its not a function!
var handle = comb.createFunctionWrapper({world : "world"}, createNewMyObject, createNewMyObject);
handle.world //=> "world"
var a = handle(1);
a.hello; //=>"hello"
a.args; //=> [1];
a = new handle(1,2);
a.hello; //=>"hello"
a.args; //=> [1,2];
Arguments
-
obj : the object to proxy
-
handler : the handler to call when the object is used as a function
-
constructTrap : the funciton to use when using new on the object
-
opts : the prototype of the object.
Source
function (obj,handler,constructTrap,opts){
var args = comb.argsToArray(arguments), ret;
if (args.length != 4) {
opts = comb.isHash(args[args.length - 1]) ? args.pop() : null;
constructTrap = comb.isFunction(args[args.length - 1]) ? args.pop() : null;
handler = comb.isFunction(args[args.length - 1]) ? args.pop() : null;
}
if (comb.isUndefined(obj)) throw new Error("obj required when using create function wrapper");
if (comb.isFunction(constructTrap) && !comb.isFunction(handler)) {
ret = Proxy.createFunction(handlerMaker(obj), constructTrap);
} else {
ret = Proxy.createFunction(handlerMaker(obj), handler, constructTrap);
}
if (opts) {
Proxy.setPrototype(ret, comb.isHash(opts) ? opts : opts.prototype);
}
return ret;
}
###executeInOrder static function public
Defined promise.js Top
This method allows one to code asynchronous code in a synchronous manner.
Using Object.define[rest of name] on objects passed will result in unexpected behavior.
Enumerating passed in object keys is not currently supported. i.e for in loops on objects.
using array enumeration methods will work though!!
Example
var staticValueFunction = function (value) {
return comb.argsToArray(arguments).join(" ");
};
var promiseValueFunction = function (value) {
var ret = new comb.Promise();
setTimeout(comb.hitch(ret, "callback", comb.argsToArray(arguments).join(" ")), 100);
return ret;
};
var hash = {
staticValueFunction:staticValueFunction,
promiseValueFunction:promiseValueFunction
};
var p = comb.executeInOrder(hash, staticValueFunction, promiseValueFunction, function (hash, staticValueFunction, promiseValueFunction) {
var toBe = staticValueFunction(promiseValueFunction("to"), "be");
var notToBe = hash.promiseValueFunction("or", hash.staticValueFunction("not", toBe));
return hash.promiseValueFunction(toBe, notToBe);
});
p.addCallback(function(ret){
console.log(ret); //=>"to be or not to be"
});
Arguments
-
args : variable number of objects.
-
cb : the function to callback to execute in order
Returns
- comb.Promise
Source
function (args,cb){
args = comb.argsToArray(arguments);
cb = comb.isFunction(args[args.length - 1]) ? args.pop() : null;
var ret = new Promise();
if (cb) {
var stack = [];
var newArgs = args.map(function (a) {
return [a, getHandler(a, stack)];
});
var cbRet = cb.apply(null, newArgs.map(function (h) {
return h[1];
}));
executeStack(stack, newArgs).then(function (results, pMap) {
if (comb.isUndefined(cbRet)) {
ret.callback(results);
}
else {
var cbResults;
if (comb.isArray(cbRet)) {
cbResults = cbRet.map(
function (arg) {
return getValueFromArrayMap(arg, pMap, newArgs);
}).filter(function (r) {
return !comb.isUndefined(r)
});
} else if (comb.isHash(cbRet)) {
cbResults = {};
for (var i in cbRet) {
cbResults[i] = getValueFromArrayMap(cbRet[i], pMap, newArgs);
}
} else if (comb.isProxy(cbRet)) {
cbResults = getValueFromArrayMap(cbRet, pMap, newArgs);
} else {
cbResults = cbRet;
}
ret.callback(cbResults);
}
}, hitch(ret, "errback"));
} else {
ret.callback();
}
return ret;
}
###handlerProxy static function public
Defined proxy.js Top
Creates a proxy for an object.
Arguments
-
obj : object to proxy
-
opts : object with methods to define on the handler.
-
proto :
Source
function (obj,opts,proto){
opts = opts || {};
if (comb.isUndefined(proto)) {
return Proxy.create(merge(handlerMaker(obj), opts));
} else {
return Proxy.create(merge(handlerMaker(obj), opts), comb.isHash(proto) ? proto : proto.prototype);
}
}
###isProxy static function public
Defined proxy.js Top
Determines if the object is a proxy or not.
Arguments
- obj : object to test
Returns
Boolean
true if it is a proxy false otherwise
Source
function (obj){
var undef;
return obj !== undef && obj !== null && Proxy.isProxy(obj);
}
###methodMissing static function public
Defined proxy.js Top
Creates a method missing proxy for an object. NOTE: This method does not gurantee that the property will be used as a function call.
Example
var x = {hello:function () {return "hello"}, world:"world"};
var xHandler = comb.methodMissing(x, function (m) {
//you can do more interesting stuff in here!
return function () {
return [m].concat(comb.argsToArray(arguments));
}
});
xHandler.hello(); //=> "hello"
xHandler.world //=> "world"
xHandler.someMethod("hello", "world"); //=> [ 'someMethod', 'hello', 'world' ]
Arguments
-
obj : object to wrap with a method missing proxy
-
handler : handle to call when a property is missing
-
proto :
-
opts
Object
: prototype to assign to the proxy
Returns
Proxy
a proxy
Source
function (obj,handler,proto){
proto = proto || {};
return Proxy.create(merge(handlerMaker(obj), noSuchMethodHandler(obj, handler)), comb.isHash(proto) ? proto : proto.prototype);
}
This plugin exposes two instance properties:
- getMissingProperty method called when a property is being retrieved and is not found on the current instance
- setMissingProperty method called when a property is being set and the property is not found on the current instance
Example
var MyClass = comb.define(comb.plugins.MethodMissing, {
instance : {
constructor : function(){
this._attributes = {};
},
getMissingProperty : function(name){
return this._attributes[name];
},
setMissingProperty : function(name, value){
return this._attributes[name] = value;
}
}
});
Instance
###Constructor
Defined plugins/MethodMissing.js Top
###getMissingProperty function public
Defined plugins/MethodMissing.js Top
Method called to retrieve a property that is not found on the current instance of the object
Arguments
- name : the name of the property to retrieve.
Source
function (name){
//return defaults
return undefined;
}
###setMissingProperty function public
Defined plugins/MethodMissing.js Top
Method called to set a property that is not found on the current instance of the object
Arguments
-
name : the name of the property to set.
-
value : the value to set the property to.
Source
function (name,value){
//return defaults
return this[name] = value;
}
MIT LICENSE
Code: git clone git://github.com/c2fo/comb-proxy.git