Ext.Function
Alternate names
Ext.util.FunctionsFiles
A collection of useful static methods to deal with function callbacks
Available since: 3.4.0
Methods
Create a new function from the provided fn, change this to the provided scope, optionally
overrides arguments for the call. (Defaults to the arguments passed by the caller)
Ext.bind is alias for Ext.Function.bind
Available since: 4.0.0
Parameters
- fn : Function
The function to delegate.
- scope : Object (optional)
The scope (
thisreference) in which the function is executed. If omitted, defaults to the default global environment object (usually the browser window). - args : Array (optional)
Overrides arguments for the call. (Defaults to the arguments passed by the caller)
- appendArgs : Boolean/Number (optional)
if True args are appended to call args instead of overriding, if a number the args are inserted at the specified position
Returns
- Function
The new function
Creates a delegate function, optionally with a bound scope which, when called, buffers the execution of the passed function for the configured number of milliseconds. If called again within that period, the impending invocation will be canceled, and the timeout period will begin again.
Available since: 4.0.0
Parameters
- fn : Function
The function to invoke on a buffered timer.
- buffer : Number
The number of milliseconds by which to buffer the invocation of the function.
- scope : Object (optional)
The scope (
thisreference) in which the passed function is executed. If omitted, defaults to the scope specified by the caller. - args : Array (optional)
Override arguments for the call. Defaults to the arguments passed by the caller.
Returns
- Function
A function which invokes the passed function after buffering for the specified time.
Creates a delegate (callback) which, when called, executes after a specific delay.
Available since: 4.0.0
Parameters
- fn : Function
The function which will be called on a delay when the returned function is called. Optionally, a replacement (or additional) argument list may be specified.
- delay : Number
The number of milliseconds to defer execution by whenever called.
- scope : Object (optional)
The scope (
thisreference) used by the function at execution time. - args : Array (optional)
Override arguments for the call. (Defaults to the arguments passed by the caller)
- appendArgs : Boolean/Number (optional)
if True args are appended to call args instead of overriding, if a number the args are inserted at the specified position.
Returns
- Function
A function which, when called, executes the original function after the specified delay.
Creates an interceptor function. The passed function is called before the original one. If it returns false, the original one is not called. The resulting function returns the results of the original function. The passed function is called with the parameters of the original function. Example usage:
var sayHi = function(name){
alert('Hi, ' + name);
}
sayHi('Fred'); // alerts "Hi, Fred"
// create a new function that validates input without
// directly modifying the original function:
var sayHiToFriend = Ext.Function.createInterceptor(sayHi, function(name){
return name == 'Brian';
});
sayHiToFriend('Fred'); // no alert
sayHiToFriend('Brian'); // alerts "Hi, Brian"
Available since: 3.4.0
Parameters
- origFn : Function
The original function.
- newFn : Function
The function to call before the original
- scope : Object (optional)
The scope (
thisreference) in which the passed function is executed. If omitted, defaults to the scope in which the original function is called or the browser window. - returnValue : Object (optional)
The value to return if the passed function return false.
Defaults to:
null
Returns
- Function
The new function
Create a combined function call sequence of the original function + the passed function. The resulting function returns the results of the original function. The passed function is called with the parameters of the original function. Example usage:
var sayHi = function(name){
alert('Hi, ' + name);
}
sayHi('Fred'); // alerts "Hi, Fred"
var sayGoodbye = Ext.Function.createSequence(sayHi, function(name){
alert('Bye, ' + name);
});
sayGoodbye('Fred'); // both alerts show
Available since: 3.4.0
Parameters
- originalFn : Function
The original function.
- newFn : Function
The function to sequence
- scope : Object (optional)
The scope (
thisreference) in which the passed function is executed. If omitted, defaults to the scope in which the original function is called or the default global environment object (usually the browser window).
Returns
- Function
The new function
Creates a throttled version of the passed function which, when called repeatedly and rapidly, invokes the passed function only after a certain interval has elapsed since the previous invocation.
This is useful for wrapping functions which may be called repeatedly, such as a handler of a mouse move event when the processing is expensive.
Available since: 4.0.0
Parameters
- fn : Function
The function to execute at a regular time interval.
- interval : Number
The interval in milliseconds on which the passed function is executed.
- scope : Object (optional)
The scope (
thisreference) in which the passed function is executed. If omitted, defaults to the scope specified by the caller.
Returns
- Function
A function which invokes the passed function at the specified interval.
Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
var sayHi = function(name){
alert('Hi, ' + name);
}
// executes immediately:
sayHi('Fred');
// executes after 2 seconds:
Ext.Function.defer(sayHi, 2000, this, ['Fred']);
// this syntax is sometimes useful for deferring
// execution of an anonymous function:
Ext.Function.defer(function(){
alert('Anonymous');
}, 100);
Ext.defer is alias for Ext.Function.defer
Available since: 3.4.0
Parameters
- fn : Function
The function to defer.
- millis : Number
The number of milliseconds for the setTimeout call (if less than or equal to 0 the function is executed immediately)
- scope : Object (optional)
The scope (
thisreference) in which the function is executed. If omitted, defaults to the browser window. - args : Array (optional)
Overrides arguments for the call. (Defaults to the arguments passed by the caller)
- appendArgs : Boolean/Number (optional)
if True args are appended to call args instead of overriding, if a number the args are inserted at the specified position
Returns
- Number
The timeout id that can be used with clearTimeout
A very commonly used method throughout the framework. It acts as a wrapper around another method
which originally accepts 2 arguments for name and value.
The wrapped function then allows "flexible" value setting of either:
nameandvalueas 2 arguments- one single object argument with multiple key - value pairs
For example:
var setValue = Ext.Function.flexSetter(function(name, value) {
this[name] = value;
});
// Afterwards
// Setting a single name - value
setValue('name1', 'value1');
// Settings multiple name - value pairs
setValue({
name1: 'value1',
name2: 'value2',
name3: 'value3'
});
Available since: 4.0.0
Parameters
- setter : Function
Returns
- Function
flexSetter
Adds behavior to an existing method that is executed after the original behavior of the function. For example:
var soup = {
contents: [],
add: function(ingredient) {
this.contents.push(ingredient);
}
};
Ext.Function.interceptAfter(soup, "add", function(ingredient){
// Always add a bit of extra salt
this.contents.push("salt");
});
soup.add("water");
soup.add("onions");
soup.contents; // will contain: water, salt, onions, salt
Available since: 4.0.6
Parameters
- object : Object
The target object
- methodName : String
Name of the method to override
- fn : Function
Function with the new behavior. It will be called with the same arguments as the original method. The return value of this function will be the return value of the new method.
- scope : Object (optional)
The scope to execute the interceptor function. Defaults to the object.
Returns
- Function
The new function just created.
Adds behavior to an existing method that is executed before the original behavior of the function. For example:
var soup = {
contents: [],
add: function(ingredient) {
this.contents.push(ingredient);
}
};
Ext.Function.interceptBefore(soup, "add", function(ingredient){
if (!this.contents.length && ingredient !== "water") {
// Always add water to start with
this.contents.push("water");
}
});
soup.add("onions");
soup.add("salt");
soup.contents; // will contain: water, onions, salt
Available since: 4.0.6
Parameters
- object : Object
The target object
- methodName : String
Name of the method to override
- fn : Function
Function with the new behavior. It will be called with the same arguments as the original method. The return value of this function will be the return value of the new method.
- scope : Object (optional)
The scope to execute the interceptor function. Defaults to the object.
Returns
- Function
The new function just created.
Create a new function from the provided fn, the arguments of which are pre-set to args.
New arguments passed to the newly created callback when it's invoked are appended after the pre-set ones.
This is especially useful when creating callbacks.
For example:
var originalFunction = function(){
alert(Ext.Array.from(arguments).join(' '));
};
var callback = Ext.Function.pass(originalFunction, ['Hello', 'World']);
callback(); // alerts 'Hello World'
callback('by Me'); // alerts 'Hello World by Me'
Ext.pass is alias for Ext.Function.pass
Available since: 4.0.0
Parameters
- fn : Function
The original function
- args : Array
The arguments to pass to new callback
- scope : Object (optional)
The scope (
thisreference) in which the function is executed.
Returns
- Function
The new callback function