Thursday, October 6, 2011

safeCall new and improved.

jQuery Project page

Some other day in the not so distant past, I found a need to have my safeCall plugin also be a safeGet as well. and this task was extremely simple. all the code had been written already for it in the safeCall function. I just had to stop it from calling it at the end and have that be the return. one other thing I noticed jQuery frowned on my attaching my mSafeCall and gSafeCall directly to the jQuery object so instead I created the safe namespace in jQuery so I took off the safe from both of them. and added the gets. and I borrowed a concept that stands at the root of jQuery; the ability to chain.

lets use the following js object for examples it shows just about everything we need it to show:

var TestObj = {
 funcRoute: {
  CallMe: function(msg) { alert(msg); },
  CallMe2: function() { 
   return { 
    NestedCall: function(msg) { 
     alert(msg); 
    },
    NestedGet: "I'm way down here!"
   }
  }
 },
 varRoute: {
  VarA: 5,
  VarB: "Hello world"
 }
};

note: the following four functions return a Safe object. so to get the return value/ value of the get you must call the object returned's value member. More on this later.

$.safe.mCall( Context, FuncPath, [...]);
this function listed above calls a function nested inside a javascript object. Starting from the Context. this is formally $.mSafeCall. the parameters are;

Context: this parameter is the starting point of the path and it is also the "this" for that function.

FuncPath: this parameter is the path to the function. starting from the context that you passed in.

Additional Params:  any additional params that you pass into the function will be passed into the function you are calling.

example: 

$.safe.mCall(TestObj,'funcRoute.CallMe','hi');

the call above will be equivalent to calling TestObj.funcRoute.CallMe('hi') with the exception that the safecall will not break if TestObj, funcRoute, or CallMe haven't been intitialized.


$.safe.gCall( Context, FuncPath, [...]);
this function listed above calls a function nested inside a javascript object. Starting from the window object. this is formally $.gSafeCall. the parameters are;

Context: this parameter is the "this" for that function.

FuncPath: this parameter is the path to the function. starting from the window object.

Additional Params:  any additional params that you pass into the function will be passed into the function you are calling.

example: 

$.safe.gCall(null,'TestObj.funcRoute.CallMe','hi');


Onward to the new functionality.

Monday, August 29, 2011

Drunks vs Goblins

your a drunk, your drink of choice? Sarsparilla!

He's a sheriff...You don't like him very much.

He interfears with your drinky time.

wait a min what about goblins?

Oh yeah you also go into a delerious state.

filled with Goblins and Sarsparilla! can you survive...

Uh...maybe?!?!


Wednesday, July 20, 2011

SafeCall JQuery

Often times when  programming in Javascript, you need to check to see if a function exists before calling it. so for instance if you are a asp.net programmer you might need to use sys.application.add_load(handler)

so if you were checking if that function esists you would need to do the following.


if(sys && sys.Application && typeof(sys.Application.add_load) == 'function')
{
      sys.Application.add_load(handler);
}

What this little snippet of code does is simplify that into:

$.gSafeCall(null,'sys.Application.add_load',handler);

what this function does is check if the function that is passed in is in fact a function and calls it passing in parameters from it's self to the function. it will start looking for the function Starting from window. (thus being the global calling)
the signature for this function is:

$.gSafeCall(context,handler [...] );

where the following parameters are:

context: the js object that represents this. so when you call the function and context is an object in the function this will be the object you passed in.

handler: this is a string that represents the path to the object starting from the window. the function will try to transverse to the function in question.

params: these are the optional parameters that should get passed into the function.that you are calling.

there also is a function called mSafeCall wich does the same thing but instead of starting from window it will start from the context and locate the function from the context that gets passed in. More on this in the code to follow