FANDOM


These two functions allow you to easily modify the behavior of any mutable methods while (optionally) preserving the old logic. Your logic may filter the method's input and/or output, and/or decide whether the old logic should be executed at all.

setCallbacksEdit

var failed = setCallbacks(object, callbacks)

Wraps specified methods on an object in callbacks. Returns a numeric array of method names which don't exist on object, or false to indicate an invalid argument.

object - must be non-null, and have a typeof value of "object" or "function". Boolean, Number, and String objects may work.

callbacks - an object of functions. Each key should be the corresponding function's name on object, and each value must be either:

  • a function, or
  • an object with properties 'func' or '0', the function, and 'when' or '1', the corresponding when value (see documentation for chain() below).

chainEdit

var success = chain(object, funcName, callback, [when = 1])

Wraps an original method in a user-supplied function. All state is transferred to the new function and may be modified as desired to affect the original, the caller, or both, or to discard the original completely. If funcName does not indicate a method of object or the arguments are otherwise invalid, false is returned; otherwise, true is returned.

object - must be non-null, and have a typeof value of "object" or "function". Boolean, Number, and String objects may work. Defaults to the global object (i.e. window) according to the ECMAScript specification.

funcName - the name used by the object to refer to the target function - NOT the actual function.

callback - your function. In all cases, this will be the passed object in both functions.

when - optional; determines when your function will be executed in relation to the original; defaults to 1 for all unrecognized values. Values are interpreted as follows:

-1 - before. Your function will be passed the arguments meant for the original. If your function returns an array, the contents will be passed to the original as the ordinary arguments.

0 - around. Your function will be passed the original function and the arguments, and will be responsible for calling the original. Your return value will be seen as the original's to outside code. (See Function.call and Function.apply.)

1 - after. Your function will be passed the original's return value and arguments. If you return a value (other than undefined), it will override the original's.

Source codeEdit

(license)

function chain(object, funcName, neu, when){
	if((typeof object != "object" && typeof object != "function") || object == null)
		return false;
	if(typeof funcName != 'string' || typeof neu != 'function')
		return false;
	if(typeof when != 'number' || when < -1 || when > 1 || when % 1 != 0)
		when = 1;
	oldFunc = object[funcName];
	if(typeof oldFunc != 'function')
		return false;
	object[funcName] = function(){
		var result1, result2, args = [];
		for(var i = 0; i < arguments.length; i++)
			args[i] = arguments[i];
 
		if(when == 1){
			result1 = oldFunc.apply(object, args);
			args.unshift(result1);
		}
 
		if(when == 0)
			args.unshift(oldFunc);
		result2 = neu.apply(object, args);
 
		if(when == -1){
			if(typeof result2 == 'object' && result2.constructor == Array)
				args = result2;
			return oldFunc.apply(object, args);
		}
 
		if(typeof result2 != 'undefined')
			return result2;
		else
			return result1;
	};
	return true;
}
function setCallbacks(object, callbacks){
	if((typeof object != "object" && typeof object != "function") || object == null)
		return false;
	if((typeof callbacks != "array" && typeof callbacks != "object") || callbacks == null)
		return false;
	var results = [];
	if(typeof callbacks != 'undefined'){
		for(i in callbacks){
			var neu = callbacks[i], when;
			if(typeof neu == 'object' && neu.constructor != Function){
				when = neu.when || neu[1];
				neu = neu.func || neu[0] || neu;
			}
			if(!chain(object, i, neu, when))
				results.push(i);
		}
	}
	return results;
}

LicensesEdit

DocumentationEdit

This file is licensed under the GFDL. Permission is granted to copy, distribute and/or modify it under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. Subject to Wikia disclaimers.


Source codeEdit

This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This work is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.