Defined index.js

Alias for comb.

createFunctionWrapper Static Function Public


Defined proxy.js

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 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

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 Returns 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).chain(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

Creates a proxy for an object.

Arguments 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

Determines if the object is a proxy or not.

Arguments Returns Source
function (obj){
   var undef;
   return obj !== undef && obj !== null && Proxy.isProxy(obj);
}
    

methodMissing Static Function Public


Defined proxy.js

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 Returns Source
function (obj,handler,proto){
   proto = proto || {};
   return Proxy.create(merge(handlerMaker(obj), noSuchMethodHandler(obj, handler)), comb.isHash(proto) ? proto : proto.prototype);
}
    

License

MIT LICENSE

Meta


Code: git clone git://github.com/c2fo/comb-proxy.git