Sencha Documentation

A collection of useful static methods to deal with function callbacks

Methods

 
alias( Object/Function object, String methodName ) : Function
Create an alias to the provided method property with name methodName of object. Note that the execution scope will st...
Create an alias to the provided method property with name methodName of object. Note that the execution scope will still be bound to the provided object itself.

Parameters

  • object : Object/Function
  • methodName : String

Returns

  • Function   aliasFn
 
bind( Function fn, [Object scope], [Array args], [Boolean/Number appendArgs] ) : Function
Create a new function from the provided fn, change this to the provided scope, optionally overrides arguments for the...
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)

Parameters

  • fn : Function
    The function to delegate.
  • scope : Object
    (optional) The scope (this reference) 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

  • Function   The new function
 
createBuffered( Function fn, Number buffer, [Object scope], [Array args] ) : Function
Creates a delegate function, optionally with a bound scope which, when called, buffers the execution of the passed fu...

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.

The resulting function is also an instance of Ext.util.DelayedTask, and so therefore implements the cancel and delay methods.

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 (this reference) 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.
 
createInterceptor( Function origFn, Function newFn, [Object scope], [Mixed returnValue] ) : Function
Creates an interceptor function. The passed function is called before the original one. If it returns false, the orig...
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"

Parameters

  • origFn : Function
    The original function.
  • newFn : Function
    The function to call before the original
  • scope : Object
    (optional) The scope (this reference) 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 : Mixed
    (optional) The value to return if the passed function return false (defaults to null).

Returns

  • Function   The new function
 
createSequence( Function origFn, Function newFn, [Object scope] ) : Function
Create a combined function call sequence of the original function + the passed function. The resulting function retur...
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

Parameters

  • origFn : Function
    The original function.
  • newFn : Function
    The function to sequence
  • scope : Object
    (optional) The scope (this reference) in which the passed function is executed. If omitted, defaults to the scope in which the original function is called or the browser window.

Returns

  • Function   The new function
 
createThrottled( fn {Function}, interval {Number}, scope (optional) ) : Void
Creates a throttled version of the passed function which, when called repeatedly and rapidly, invokes the passed func...

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.

Parameters

  • {Function} : fn
    The function to execute at a regular time interval.
  • {Number} : interval
    The interval in milliseconds on which the passed function is executed.
  • (optional) : scope
    The scope (this reference) in which the passed function is executed. If omitted, defaults to the scope specified by the caller.

Returns

  • Void
 
defer( Function fn, Number millis, [Object scope], [Array args], [Boolean/Number appendArgs] ) : Number
Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage: var say...
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);

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 (this reference) 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
 
flexSetter( Function setter ) : Void
A very commonly used method throughout the framework. It acts as a wrapper around another method which originally acc...
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:
  • name and value as 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'
});

Parameters

  • setter : Function

Returns

  • Void
 
pass : Function
Creates a callback that passes arguments[0], arguments[1], arguments[2], ... Call directly on any function. Example: ...
Creates a callback that passes arguments[0], arguments[1], arguments[2], ... Call directly on any function. Example: Ext.pass(myFunction, arg1, arg2) Will create a function that is bound to those 2 args. If a specific scope is required in the callback, use Ext.Function.bind instead. The function returned by 'pass' always executes in the window scope.

This method is required when you want to pass arguments to a callback function. If no arguments are needed, you can simply pass a reference to the function as a callback (e.g., callback: myFn). However, if you tried to pass a function with arguments (e.g., callback: myFn(arg1, arg2)) the function would simply execute immediately when the code is parsed. Example usage:

var sayHi = function(name){
   alert('Hi, ' + name);
}

// clicking the button alerts "Hi, Fred"
new Ext.Button({
    text: 'Say Hi',
    renderTo: Ext.getBody(),
    handler: Ext.pass(sayHi, 'Fred')
});