1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-Function'>/**
2 </span> * @class Ext.Function
4 * A collection of useful static methods to deal with function callbacks
10 <span id='Ext-Function-method-flexSetter'> /**
11 </span> * A very commonly used method throughout the framework. It acts as a wrapper around another method
12 * which originally accepts 2 arguments for <code>name</code> and <code>value</code>.
13 * The wrapped function then allows "flexible" value setting of either:
16 * <li><code>name</code> and <code>value</code> as 2 arguments</li>
17 * <li>one single object argument with multiple key - value pairs</li>
21 * <pre><code>
22 var setValue = Ext.Function.flexSetter(function(name, value) {
27 // Setting a single name - value
28 setValue('name1', 'value1');
30 // Settings multiple name - value pairs
36 * </code></pre>
37 * @param {Function} setter
38 * @returns {Function} flexSetter
40 flexSetter: function(fn) {
41 return function(a, b) {
48 if (typeof a !== 'string') {
50 if (a.hasOwnProperty(k)) {
51 fn.call(this, k, a[k]);
55 if (Ext.enumerables) {
56 for (i = Ext.enumerables.length; i--;) {
57 k = Ext.enumerables[i];
58 if (a.hasOwnProperty(k)) {
59 fn.call(this, k, a[k]);
71 <span id='Ext-Function-method-bind'> /**
72 </span> * Create a new function from the provided <code>fn</code>, change <code>this</code> to the provided scope, optionally
73 * overrides arguments for the call. (Defaults to the arguments passed by the caller)
75 * @param {Function} fn The function to delegate.
76 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
77 * <b>If omitted, defaults to the browser window.</b>
78 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
79 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
80 * if a number the args are inserted at the specified position
81 * @return {Function} The new function
83 bind: function(fn, scope, args, appendArgs) {
88 var callArgs = args || arguments;
90 if (appendArgs === true) {
91 callArgs = Array.prototype.slice.call(arguments, 0);
92 callArgs = callArgs.concat(args);
94 else if (Ext.isNumber(appendArgs)) {
95 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
96 applyArgs = [appendArgs, 0].concat(args); // create method call params
97 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
100 return method.apply(scope || window, callArgs);
104 <span id='Ext-Function-method-pass'> /**
105 </span> * Create a new function from the provided <code>fn</code>, the arguments of which are pre-set to `args`.
106 * New arguments passed to the newly created callback when it's invoked are appended after the pre-set ones.
107 * This is especially useful when creating callbacks.
110 var originalFunction = function(){
111 alert(Ext.Array.from(arguments).join(' '));
114 var callback = Ext.Function.pass(originalFunction, ['Hello', 'World']);
116 callback(); // alerts 'Hello World'
117 callback('by Me'); // alerts 'Hello World by Me'
119 * @param {Function} fn The original function
120 * @param {Array} args The arguments to pass to new callback
121 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
122 * @return {Function} The new callback function
124 pass: function(fn, args, scope) {
126 args = Ext.Array.from(args);
130 return fn.apply(scope, args.concat(Ext.Array.toArray(arguments)));
134 <span id='Ext-Function-method-alias'> /**
135 </span> * Create an alias to the provided method property with name <code>methodName</code> of <code>object</code>.
136 * Note that the execution scope will still be bound to the provided <code>object</code> itself.
138 * @param {Object/Function} object
139 * @param {String} methodName
140 * @return {Function} aliasFn
142 alias: function(object, methodName) {
144 return object[methodName].apply(object, arguments);
148 <span id='Ext-Function-method-createInterceptor'> /**
149 </span> * Creates an interceptor function. The passed function is called before the original one. If it returns false,
150 * the original one is not called. The resulting function returns the results of the original function.
151 * The passed function is called with the parameters of the original function. Example usage:
152 * <pre><code>
153 var sayHi = function(name){
154 alert('Hi, ' + name);
157 sayHi('Fred'); // alerts "Hi, Fred"
159 // create a new function that validates input without
160 // directly modifying the original function:
161 var sayHiToFriend = Ext.Function.createInterceptor(sayHi, function(name){
162 return name == 'Brian';
165 sayHiToFriend('Fred'); // no alert
166 sayHiToFriend('Brian'); // alerts "Hi, Brian"
167 </code></pre>
168 * @param {Function} origFn The original function.
169 * @param {Function} newFn The function to call before the original
170 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the passed function is executed.
171 * <b>If omitted, defaults to the scope in which the original function is called or the browser window.</b>
172 * @param {Mixed} returnValue (optional) The value to return if the passed function return false (defaults to null).
173 * @return {Function} The new function
175 createInterceptor: function(origFn, newFn, scope, returnValue) {
177 if (!Ext.isFunction(newFn)) {
185 newFn.method = origFn;
186 return (newFn.apply(scope || me || window, args) !== false) ? origFn.apply(me || window, args) : returnValue || null;
191 <span id='Ext-Function-method-createDelayed'> /**
192 </span> * Creates a delegate (callback) which, when called, executes after a specific delay.
193 * @param {Function} fn The function which will be called on a delay when the returned function is called.
194 * Optionally, a replacement (or additional) argument list may be specified.
195 * @param {Number} delay The number of milliseconds to defer execution by whenever called.
196 * @param {Object} scope (optional) The scope (<code>this</code> reference) used by the function at execution time.
197 * @param {Array} args (optional) Override arguments for the call. (Defaults to the arguments passed by the caller)
198 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
199 * if a number the args are inserted at the specified position.
200 * @return {Function} A function which, when called, executes the original function after the specified delay.
202 createDelayed: function(fn, delay, scope, args, appendArgs) {
204 fn = Ext.Function.bind(fn, scope, args, appendArgs);
208 setTimeout(function() {
209 fn.apply(me, arguments);
214 <span id='Ext-Function-method-defer'> /**
215 </span> * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
216 * <pre><code>
217 var sayHi = function(name){
218 alert('Hi, ' + name);
221 // executes immediately:
224 // executes after 2 seconds:
225 Ext.Function.defer(sayHi, 2000, this, ['Fred']);
227 // this syntax is sometimes useful for deferring
228 // execution of an anonymous function:
229 Ext.Function.defer(function(){
232 </code></pre>
233 * @param {Function} fn The function to defer.
234 * @param {Number} millis The number of milliseconds for the setTimeout call (if less than or equal to 0 the function is executed immediately)
235 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
236 * <b>If omitted, defaults to the browser window.</b>
237 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
238 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
239 * if a number the args are inserted at the specified position
240 * @return {Number} The timeout id that can be used with clearTimeout
242 defer: function(fn, millis, obj, args, appendArgs) {
243 fn = Ext.Function.bind(fn, obj, args, appendArgs);
245 return setTimeout(fn, millis);
251 <span id='Ext-Function-method-createSequence'> /**
252 </span> * Create a combined function call sequence of the original function + the passed function.
253 * The resulting function returns the results of the original function.
254 * The passed function is called with the parameters of the original function. Example usage:
256 * <pre><code>
257 var sayHi = function(name){
258 alert('Hi, ' + name);
261 sayHi('Fred'); // alerts "Hi, Fred"
263 var sayGoodbye = Ext.Function.createSequence(sayHi, function(name){
264 alert('Bye, ' + name);
267 sayGoodbye('Fred'); // both alerts show
268 * </code></pre>
270 * @param {Function} origFn The original function.
271 * @param {Function} newFn The function to sequence
272 * @param {Object} scope (optional) The scope (this reference) in which the passed function is executed.
273 * If omitted, defaults to the scope in which the original function is called or the browser window.
274 * @return {Function} The new function
276 createSequence: function(origFn, newFn, scope) {
277 if (!Ext.isFunction(newFn)) {
282 var retval = origFn.apply(this || window, arguments);
283 newFn.apply(scope || this || window, arguments);
289 <span id='Ext-Function-method-createBuffered'> /**
290 </span> * <p>Creates a delegate function, optionally with a bound scope which, when called, buffers
291 * the execution of the passed function for the configured number of milliseconds.
292 * If called again within that period, the impending invocation will be canceled, and the
293 * timeout period will begin again.</p>
295 * @param {Function} fn The function to invoke on a buffered timer.
296 * @param {Number} buffer The number of milliseconds by which to buffer the invocation of the
298 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which
299 * the passed function is executed. If omitted, defaults to the scope specified by the caller.
300 * @param {Array} args (optional) Override arguments for the call. Defaults to the arguments
301 * passed by the caller.
302 * @return {Function} A function which invokes the passed function after buffering for the specified time.
304 createBuffered: function(fn, buffer, scope, args) {
310 clearInterval(timerId);
313 timerId = setTimeout(function(){
314 fn.apply(scope || me, args || arguments);
320 <span id='Ext-Function-method-createThrottled'> /**
321 </span> * <p>Creates a throttled version of the passed function which, when called repeatedly and
322 * rapidly, invokes the passed function only after a certain interval has elapsed since the
323 * previous invocation.</p>
325 * <p>This is useful for wrapping functions which may be called repeatedly, such as
326 * a handler of a mouse move event when the processing is expensive.</p>
328 * @param fn {Function} The function to execute at a regular time interval.
329 * @param interval {Number} The interval <b>in milliseconds</b> on which the passed function is executed.
330 * @param scope (optional) The scope (<code><b>this</b></code> reference) in which
331 * the passed function is executed. If omitted, defaults to the scope specified by the caller.
332 * @returns {Function} A function which invokes the passed function at the specified interval.
334 createThrottled: function(fn, interval, scope) {
335 var lastCallTime, elapsed, lastArgs, timer, execute = function() {
336 fn.apply(scope || this, lastArgs);
337 lastCallTime = new Date().getTime();
341 elapsed = new Date().getTime() - lastCallTime;
342 lastArgs = arguments;
345 if (!lastCallTime || (elapsed >= interval)) {
348 timer = setTimeout(execute, interval - elapsed);
354 <span id='Ext-method-defer'>/**
355 </span> * Shorthand for {@link Ext.Function#defer}
359 Ext.defer = Ext.Function.alias(Ext.Function, 'defer');
361 <span id='Ext-method-pass'>/**
362 </span> * Shorthand for {@link Ext.Function#pass}
366 Ext.pass = Ext.Function.alias(Ext.Function, 'pass');
368 <span id='Ext-method-bind'>/**
369 </span> * Shorthand for {@link Ext.Function#bind}
373 Ext.bind = Ext.Function.alias(Ext.Function, 'bind');
374 </pre></pre></body></html>