3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
18 * A collection of useful static methods to deal with function callbacks
24 * A very commonly used method throughout the framework. It acts as a wrapper around another method
25 * which originally accepts 2 arguments for `name` and `value`.
26 * The wrapped function then allows "flexible" value setting of either:
28 * - `name` and `value` as 2 arguments
29 * - one single object argument with multiple key - value pairs
33 * var setValue = Ext.Function.flexSetter(function(name, value) {
38 * // Setting a single name - value
39 * setValue('name1', 'value1');
41 * // Settings multiple name - value pairs
48 * @param {Function} setter
49 * @returns {Function} flexSetter
51 flexSetter: function(fn) {
52 return function(a, b) {
59 if (typeof a !== 'string') {
61 if (a.hasOwnProperty(k)) {
62 fn.call(this, k, a[k]);
66 if (Ext.enumerables) {
67 for (i = Ext.enumerables.length; i--;) {
68 k = Ext.enumerables[i];
69 if (a.hasOwnProperty(k)) {
70 fn.call(this, k, a[k]);
83 * Create a new function from the provided `fn`, change `this` to the provided scope, optionally
84 * overrides arguments for the call. (Defaults to the arguments passed by the caller)
86 * {@link Ext#bind Ext.bind} is alias for {@link Ext.Function#bind Ext.Function.bind}
88 * @param {Function} fn The function to delegate.
89 * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed.
90 * **If omitted, defaults to the browser window.**
91 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
92 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
93 * if a number the args are inserted at the specified position
94 * @return {Function} The new function
96 bind: function(fn, scope, args, appendArgs) {
98 slice = Array.prototype.slice;
101 var callArgs = args || arguments;
103 if (appendArgs === true) {
104 callArgs = slice.call(arguments, 0);
105 callArgs = callArgs.concat(args);
107 else if (Ext.isNumber(appendArgs)) {
108 callArgs = slice.call(arguments, 0); // copy arguments first
109 Ext.Array.insert(callArgs, appendArgs, args);
112 return method.apply(scope || window, callArgs);
117 * Create a new function from the provided `fn`, the arguments of which are pre-set to `args`.
118 * New arguments passed to the newly created callback when it's invoked are appended after the pre-set ones.
119 * This is especially useful when creating callbacks.
123 * var originalFunction = function(){
124 * alert(Ext.Array.from(arguments).join(' '));
127 * var callback = Ext.Function.pass(originalFunction, ['Hello', 'World']);
129 * callback(); // alerts 'Hello World'
130 * callback('by Me'); // alerts 'Hello World by Me'
132 * {@link Ext#pass Ext.pass} is alias for {@link Ext.Function#pass Ext.Function.pass}
134 * @param {Function} fn The original function
135 * @param {Array} args The arguments to pass to new callback
136 * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed.
137 * @return {Function} The new callback function
139 pass: function(fn, args, scope) {
141 args = Ext.Array.from(args);
145 return fn.apply(scope, args.concat(Ext.Array.toArray(arguments)));
150 * Create an alias to the provided method property with name `methodName` of `object`.
151 * Note that the execution scope will still be bound to the provided `object` itself.
153 * @param {Object/Function} object
154 * @param {String} methodName
155 * @return {Function} aliasFn
157 alias: function(object, methodName) {
159 return object[methodName].apply(object, arguments);
164 * Creates an interceptor function. The passed function is called before the original one. If it returns false,
165 * the original one is not called. The resulting function returns the results of the original function.
166 * The passed function is called with the parameters of the original function. Example usage:
168 * var sayHi = function(name){
169 * alert('Hi, ' + name);
172 * sayHi('Fred'); // alerts "Hi, Fred"
174 * // create a new function that validates input without
175 * // directly modifying the original function:
176 * var sayHiToFriend = Ext.Function.createInterceptor(sayHi, function(name){
177 * return name == 'Brian';
180 * sayHiToFriend('Fred'); // no alert
181 * sayHiToFriend('Brian'); // alerts "Hi, Brian"
183 * @param {Function} origFn The original function.
184 * @param {Function} newFn The function to call before the original
185 * @param {Object} scope (optional) The scope (`this` reference) in which the passed function is executed.
186 * **If omitted, defaults to the scope in which the original function is called or the browser window.**
187 * @param {Mixed} returnValue (optional) The value to return if the passed function return false (defaults to null).
188 * @return {Function} The new function
190 createInterceptor: function(origFn, newFn, scope, returnValue) {
192 if (!Ext.isFunction(newFn)) {
200 newFn.method = origFn;
201 return (newFn.apply(scope || me || window, args) !== false) ? origFn.apply(me || window, args) : returnValue || null;
207 * Creates a delegate (callback) which, when called, executes after a specific delay.
209 * @param {Function} fn The function which will be called on a delay when the returned function is called.
210 * Optionally, a replacement (or additional) argument list may be specified.
211 * @param {Number} delay The number of milliseconds to defer execution by whenever called.
212 * @param {Object} scope (optional) The scope (`this` reference) used by the function at execution time.
213 * @param {Array} args (optional) Override arguments for the call. (Defaults to the arguments passed by the caller)
214 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
215 * if a number the args are inserted at the specified position.
216 * @return {Function} A function which, when called, executes the original function after the specified delay.
218 createDelayed: function(fn, delay, scope, args, appendArgs) {
220 fn = Ext.Function.bind(fn, scope, args, appendArgs);
224 setTimeout(function() {
225 fn.apply(me, arguments);
231 * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
233 * var sayHi = function(name){
234 * alert('Hi, ' + name);
237 * // executes immediately:
240 * // executes after 2 seconds:
241 * Ext.Function.defer(sayHi, 2000, this, ['Fred']);
243 * // this syntax is sometimes useful for deferring
244 * // execution of an anonymous function:
245 * Ext.Function.defer(function(){
246 * alert('Anonymous');
249 * {@link Ext#defer Ext.defer} is alias for {@link Ext.Function#defer Ext.Function.defer}
251 * @param {Function} fn The function to defer.
252 * @param {Number} millis The number of milliseconds for the setTimeout call
253 * (if less than or equal to 0 the function is executed immediately)
254 * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed.
255 * **If omitted, defaults to the browser window.**
256 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
257 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
258 * if a number the args are inserted at the specified position
259 * @return {Number} The timeout id that can be used with clearTimeout
261 defer: function(fn, millis, obj, args, appendArgs) {
262 fn = Ext.Function.bind(fn, obj, args, appendArgs);
264 return setTimeout(fn, millis);
271 * Create a combined function call sequence of the original function + the passed function.
272 * The resulting function returns the results of the original function.
273 * The passed function is called with the parameters of the original function. Example usage:
275 * var sayHi = function(name){
276 * alert('Hi, ' + name);
279 * sayHi('Fred'); // alerts "Hi, Fred"
281 * var sayGoodbye = Ext.Function.createSequence(sayHi, function(name){
282 * alert('Bye, ' + name);
285 * sayGoodbye('Fred'); // both alerts show
287 * @param {Function} origFn The original function.
288 * @param {Function} newFn The function to sequence
289 * @param {Object} scope (optional) The scope (`this` reference) in which the passed function is executed.
290 * If omitted, defaults to the scope in which the original function is called or the browser window.
291 * @return {Function} The new function
293 createSequence: function(origFn, newFn, scope) {
294 if (!Ext.isFunction(newFn)) {
299 var retval = origFn.apply(this || window, arguments);
300 newFn.apply(scope || this || window, arguments);
307 * Creates a delegate function, optionally with a bound scope which, when called, buffers
308 * the execution of the passed function for the configured number of milliseconds.
309 * If called again within that period, the impending invocation will be canceled, and the
310 * timeout period will begin again.
312 * @param {Function} fn The function to invoke on a buffered timer.
313 * @param {Number} buffer The number of milliseconds by which to buffer the invocation of the
315 * @param {Object} scope (optional) The scope (`this` reference) in which
316 * the passed function is executed. If omitted, defaults to the scope specified by the caller.
317 * @param {Array} args (optional) Override arguments for the call. Defaults to the arguments
318 * passed by the caller.
319 * @return {Function} A function which invokes the passed function after buffering for the specified time.
321 createBuffered: function(fn, buffer, scope, args) {
327 clearInterval(timerId);
330 timerId = setTimeout(function(){
331 fn.apply(scope || me, args || arguments);
338 * Creates a throttled version of the passed function which, when called repeatedly and
339 * rapidly, invokes the passed function only after a certain interval has elapsed since the
340 * previous invocation.
342 * This is useful for wrapping functions which may be called repeatedly, such as
343 * a handler of a mouse move event when the processing is expensive.
345 * @param {Function} fn The function to execute at a regular time interval.
346 * @param {Number} interval The interval **in milliseconds** on which the passed function is executed.
347 * @param {Object} scope (optional) The scope (`this` reference) in which
348 * the passed function is executed. If omitted, defaults to the scope specified by the caller.
349 * @returns {Function} A function which invokes the passed function at the specified interval.
351 createThrottled: function(fn, interval, scope) {
352 var lastCallTime, elapsed, lastArgs, timer, execute = function() {
353 fn.apply(scope || this, lastArgs);
354 lastCallTime = new Date().getTime();
358 elapsed = new Date().getTime() - lastCallTime;
359 lastArgs = arguments;
362 if (!lastCallTime || (elapsed >= interval)) {
365 timer = setTimeout(execute, interval - elapsed);
374 * @alias Ext.Function#defer
376 Ext.defer = Ext.Function.alias(Ext.Function, 'defer');
381 * @alias Ext.Function#pass
383 Ext.pass = Ext.Function.alias(Ext.Function, 'pass');
388 * @alias Ext.Function#bind
390 Ext.bind = Ext.Function.alias(Ext.Function, 'bind');