Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / api / Ext.Function.html
1 <!DOCTYPE html><html><head><title>Ext.Function | Ext JS 4.0 Documentation</title><script type="text/javascript" src="../ext-all.js"></script><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../scrollbars.css" type="text/css"><link rel="stylesheet" href="../docs.css" type="text/css"><link id="styleCss" rel="stylesheet" href="../style.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script><link rel="stylesheet" href="../prettify.css" type="text/css"><!-- link(rel: 'stylesheet', href: req.baseURL + '/css/ext4.css', type: 'text/css')--><link rel="shortcut icon" type="image/ico" href="../favicon.ico"><!--[if IE]>
2 <style type="text/css">.head-band { display: none; }
3 .header { border: 0; top: 0; left: 0px; background: url(../header.gif) repeat-x; }
4 .doc-tab .members .member a.more { background-color: #efefef; }
5 </style><link rel="stylesheet" href="/new/css/ie.css" type="text/css"><![endif]-->
6 </head><body id="ext-body" class="iScroll"><div id="notice" class="notice">For up to date documentation and features, visit 
7 <a href="http://docs.sencha.com/ext-js/4-0">http://docs.sencha.com/ext-js/4-0</a></div><div class="wrapper"><div class="head-band"></div><div class="header"><h2><a href="../index.html">Sencha Documentation</a></h2></div><div id="search"><form><input type="text" placeholder="Search" id="search-field" autocomplete="off" name="q"></form><div id="search-box"></div></div><div id="treePanel"></div><div id="container"><script type="text/javascript">
8
9     req = {
10         liveURL: '.',
11         standAloneMode: true,
12         origDocClass: 'Ext.Function',
13         docClass: 'Ext.Function',
14         docReq: 'Ext.Function',
15         version: '4.0',
16         baseURL: '.',
17         baseDocURL: '.',
18         baseProdURL: '.'
19     };
20
21     clsInfo = {};
22
23
24
25 </script>
26
27 <script type="text/javascript" src="../search.js"></script>
28 <!--script type="text/javascript" src="/new/javascripts/app/examples.js"></script-->
29 <script type="text/javascript" src="../class_tree.js"></script>
30 <script type="text/javascript" src="../class_doc.js"></script>
31 <script type="text/javascript">
32     req.source = 'Function.html#Ext-Function';
33     clsInfo = {"methods":["alias","bind","createBuffered","createDelayed","createInterceptor","createSequence","createThrottled","defer","flexSetter","pass"],"cfgs":[],"properties":[],"events":[],"subclasses":[]};
34     Ext.onReady(function() {
35         Ext.create('Docs.classPanel');
36     });
37 </script><div id="top-block" class="top-block"><h1 id="clsTitle" class="cls"><a href="../source/Function.html#Ext-Function" target="_blank">Ext.Function</a></h1></div><div id="docContent"><div id="doc-overview-content"><div class="lft"><p>A collection of useful static methods to deal with function callbacks</p>
38 <div class="members"><div class="m-methods"><a name="methods"></a><div class="definedBy">Defined By</div><h3 class="mth p">Methods</h3><div id="method-alias" class="member f ni"><a href="Ext.Function.html#method-alias" rel="method-alias" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-alias" class="viewSource">view source</a></div><a name="alias"></a><a name="method-alias"></a><a href="Ext.Function.html#" rel="method-alias" class="cls expand">alias</a>(
39 <span class="pre">Object/Function object, String methodName</span>)
40  : Function</div><div class="description"><div class="short">Create an alias to the provided method property with name methodName of object.
41 Note that the execution scope will st...</div><div class="long"><p>Create an alias to the provided method property with name <code>methodName</code> of <code>object</code>.
42 Note that the execution scope will still be bound to the provided <code>object</code> itself.</p>
43 <h3 class="pa">Parameters</h3><ul><li><span class="pre">object</span> : Object/Function<div class="sub-desc">
44 </div></li><li><span class="pre">methodName</span> : String<div class="sub-desc">
45 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>aliasFn</p>
46 </li></ul></div></div></div><div id="method-bind" class="member ni"><a href="Ext.Function.html#method-bind" rel="method-bind" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-bind" class="viewSource">view source</a></div><a name="bind"></a><a name="method-bind"></a><a href="Ext.Function.html#" rel="method-bind" class="cls expand">bind</a>(
47 <span class="pre">Function fn, [Object scope], [Array args], [Boolean/Number appendArgs]</span>)
48  : Function</div><div class="description"><div class="short">Create a new function from the provided fn, change this to the provided scope, optionally
49 overrides arguments for the...</div><div class="long"><p>Create a new function from the provided <code>fn</code>, change <code>this</code> to the provided scope, optionally
50 overrides arguments for the call. (Defaults to the arguments passed by the caller)</p>
51 <h3 class="pa">Parameters</h3><ul><li><span class="pre">fn</span> : Function<div class="sub-desc"><p>The function to delegate.</p>
52 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
53 <b>If omitted, defaults to the browser window.</b></p>
54 </div></li><li><span class="pre">args</span> : Array<div class="sub-desc"><p>(optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)</p>
55 </div></li><li><span class="pre">appendArgs</span> : Boolean/Number<div class="sub-desc"><p>(optional) if True args are appended to call args instead of overriding,
56 if a number the args are inserted at the specified position</p>
57 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>The new function</p>
58 </li></ul></div></div></div><div id="method-createBuffered" class="member ni"><a href="Ext.Function.html#method-createBuffered" rel="method-createBuffered" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-createBuffered" class="viewSource">view source</a></div><a name="createBuffered"></a><a name="method-createBuffered"></a><a href="Ext.Function.html#" rel="method-createBuffered" class="cls expand">createBuffered</a>(
59 <span class="pre">Function fn, Number buffer, [Object scope], [Array args]</span>)
60  : Function</div><div class="description"><div class="short">Creates a delegate function, optionally with a bound scope which, when called, buffers
61 the execution of the passed fu...</div><div class="long"><p>Creates a delegate function, optionally with a bound scope which, when called, buffers
62 the execution of the passed function for the configured number of milliseconds.
63 If called again within that period, the impending invocation will be canceled, and the
64 timeout period will begin again.</p>
65
66 <h3 class="pa">Parameters</h3><ul><li><span class="pre">fn</span> : Function<div class="sub-desc"><p>The function to invoke on a buffered timer.</p>
67 </div></li><li><span class="pre">buffer</span> : Number<div class="sub-desc"><p>The number of milliseconds by which to buffer the invocation of the
68 function.</p>
69 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code><b>this</b></code> reference) in which
70 the passed function is executed. If omitted, defaults to the scope specified by the caller.</p>
71 </div></li><li><span class="pre">args</span> : Array<div class="sub-desc"><p>(optional) Override arguments for the call. Defaults to the arguments
72 passed by the caller.</p>
73 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>A function which invokes the passed function after buffering for the specified time.</p>
74 </li></ul></div></div></div><div id="method-createDelayed" class="member ni"><a href="Ext.Function.html#method-createDelayed" rel="method-createDelayed" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-createDelayed" class="viewSource">view source</a></div><a name="createDelayed"></a><a name="method-createDelayed"></a><a href="Ext.Function.html#" rel="method-createDelayed" class="cls expand">createDelayed</a>(
75 <span class="pre">Function fn, Number delay, [Object scope], [Array args], [Boolean/Number appendArgs]</span>)
76  : Function</div><div class="description"><div class="short"><p>Creates a delegate (callback) which, when called, executes after a specific delay.</p>
77 </div><div class="long"><p>Creates a delegate (callback) which, when called, executes after a specific delay.</p>
78 <h3 class="pa">Parameters</h3><ul><li><span class="pre">fn</span> : Function<div class="sub-desc"><p>The function which will be called on a delay when the returned function is called.
79 Optionally, a replacement (or additional) argument list may be specified.</p>
80 </div></li><li><span class="pre">delay</span> : Number<div class="sub-desc"><p>The number of milliseconds to defer execution by whenever called.</p>
81 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code>this</code> reference) used by the function at execution time.</p>
82 </div></li><li><span class="pre">args</span> : Array<div class="sub-desc"><p>(optional) Override arguments for the call. (Defaults to the arguments passed by the caller)</p>
83 </div></li><li><span class="pre">appendArgs</span> : Boolean/Number<div class="sub-desc"><p>(optional) if True args are appended to call args instead of overriding,
84 if a number the args are inserted at the specified position.</p>
85 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>A function which, when called, executes the original function after the specified delay.</p>
86 </li></ul></div></div></div><div id="method-createInterceptor" class="member ni"><a href="Ext.Function.html#method-createInterceptor" rel="method-createInterceptor" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-createInterceptor" class="viewSource">view source</a></div><a name="createInterceptor"></a><a name="method-createInterceptor"></a><a href="Ext.Function.html#" rel="method-createInterceptor" class="cls expand">createInterceptor</a>(
87 <span class="pre">Function origFn, Function newFn, [Object scope], [Mixed returnValue]</span>)
88  : Function</div><div class="description"><div class="short">Creates an interceptor function. The passed function is called before the original one. If it returns false,
89 the orig...</div><div class="long"><p>Creates an interceptor function. The passed function is called before the original one. If it returns false,
90 the original one is not called. The resulting function returns the results of the original function.
91 The passed function is called with the parameters of the original function. Example usage:</p>
92
93 <pre><code>var sayHi = function(name){
94     alert('Hi, ' + name);
95 }
96
97 sayHi('Fred'); // alerts "Hi, Fred"
98
99 // create a new function that validates input without
100 // directly modifying the original function:
101 var sayHiToFriend = Ext.Function.createInterceptor(sayHi, function(name){
102     return name == 'Brian';
103 });
104
105 sayHiToFriend('Fred');  // no alert
106 sayHiToFriend('Brian'); // alerts "Hi, Brian"
107      </code></pre>
108
109 <h3 class="pa">Parameters</h3><ul><li><span class="pre">origFn</span> : Function<div class="sub-desc"><p>The original function.</p>
110 </div></li><li><span class="pre">newFn</span> : Function<div class="sub-desc"><p>The function to call before the original</p>
111 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code><b>this</b></code> reference) in which the passed function is executed.
112 <b>If omitted, defaults to the scope in which the original function is called or the browser window.</b></p>
113 </div></li><li><span class="pre">returnValue</span> : Mixed<div class="sub-desc"><p>(optional) The value to return if the passed function return false (defaults to null).</p>
114 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>The new function</p>
115 </li></ul></div></div></div><div id="method-createSequence" class="member ni"><a href="Ext.Function.html#method-createSequence" rel="method-createSequence" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-createSequence" class="viewSource">view source</a></div><a name="createSequence"></a><a name="method-createSequence"></a><a href="Ext.Function.html#" rel="method-createSequence" class="cls expand">createSequence</a>(
116 <span class="pre">Function origFn, Function newFn, [Object scope]</span>)
117  : Function</div><div class="description"><div class="short">Create a combined function call sequence of the original function + the passed function.
118 The resulting function retur...</div><div class="long"><p>Create a combined function call sequence of the original function + the passed function.
119 The resulting function returns the results of the original function.
120 The passed function is called with the parameters of the original function. Example usage:</p>
121
122 <pre><code>var sayHi = function(name){
123     alert('Hi, ' + name);
124 }
125
126 sayHi('Fred'); // alerts "Hi, Fred"
127
128 var sayGoodbye = Ext.Function.createSequence(sayHi, function(name){
129     alert('Bye, ' + name);
130 });
131
132 sayGoodbye('Fred'); // both alerts show
133 </code></pre>
134
135 <h3 class="pa">Parameters</h3><ul><li><span class="pre">origFn</span> : Function<div class="sub-desc"><p>The original function.</p>
136 </div></li><li><span class="pre">newFn</span> : Function<div class="sub-desc"><p>The function to sequence</p>
137 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (this reference) in which the passed function is executed.
138 If omitted, defaults to the scope in which the original function is called or the browser window.</p>
139 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>The new function</p>
140 </li></ul></div></div></div><div id="method-createThrottled" class="member ni"><a href="Ext.Function.html#method-createThrottled" rel="method-createThrottled" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-createThrottled" class="viewSource">view source</a></div><a name="createThrottled"></a><a name="method-createThrottled"></a><a href="Ext.Function.html#" rel="method-createThrottled" class="cls expand">createThrottled</a>(
141 <span class="pre">Object fn, Object interval, [Object scope]</span>)
142  : Function</div><div class="description"><div class="short">Creates a throttled version of the passed function which, when called repeatedly and
143 rapidly, invokes the passed func...</div><div class="long"><p>Creates a throttled version of the passed function which, when called repeatedly and
144 rapidly, invokes the passed function only after a certain interval has elapsed since the
145 previous invocation.</p>
146
147
148
149
150 <p>This is useful for wrapping functions which may be called repeatedly, such as
151 a handler of a mouse move event when the processing is expensive.</p>
152
153 <h3 class="pa">Parameters</h3><ul><li><span class="pre">fn</span> : Object<div class="sub-desc"><p>{Function} The function to execute at a regular time interval.</p>
154 </div></li><li><span class="pre">interval</span> : Object<div class="sub-desc"><p>{Number} The interval <b>in milliseconds</b> on which the passed function is executed.</p>
155 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code><b>this</b></code> reference) in which
156 the passed function is executed. If omitted, defaults to the scope specified by the caller.</p>
157 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>A function which invokes the passed function at the specified interval.</p>
158 </li></ul></div></div></div><div id="method-defer" class="member ni"><a href="Ext.Function.html#method-defer" rel="method-defer" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-defer" class="viewSource">view source</a></div><a name="defer"></a><a name="method-defer"></a><a href="Ext.Function.html#" rel="method-defer" class="cls expand">defer</a>(
159 <span class="pre">Function fn, Number millis, [Object scope], [Array args], [Boolean/Number appendArgs]</span>)
160  : Number</div><div class="description"><div class="short">Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
161
162 var sa...</div><div class="long"><p>Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:</p>
163
164 <pre><code>var sayHi = function(name){
165     alert('Hi, ' + name);
166 }
167
168 // executes immediately:
169 sayHi('Fred');
170
171 // executes after 2 seconds:
172 Ext.Function.defer(sayHi, 2000, this, ['Fred']);
173
174 // this syntax is sometimes useful for deferring
175 // execution of an anonymous function:
176 Ext.Function.defer(function(){
177     alert('Anonymous');
178 }, 100);
179      </code></pre>
180
181 <h3 class="pa">Parameters</h3><ul><li><span class="pre">fn</span> : Function<div class="sub-desc"><p>The function to defer.</p>
182 </div></li><li><span class="pre">millis</span> : Number<div class="sub-desc"><p>The number of milliseconds for the setTimeout call (if less than or equal to 0 the function is executed immediately)</p>
183 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
184 <b>If omitted, defaults to the browser window.</b></p>
185 </div></li><li><span class="pre">args</span> : Array<div class="sub-desc"><p>(optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)</p>
186 </div></li><li><span class="pre">appendArgs</span> : Boolean/Number<div class="sub-desc"><p>(optional) if True args are appended to call args instead of overriding,
187 if a number the args are inserted at the specified position</p>
188 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Number</span>&nbsp; &nbsp;<p>The timeout id that can be used with clearTimeout</p>
189 </li></ul></div></div></div><div id="method-flexSetter" class="member ni"><a href="Ext.Function.html#method-flexSetter" rel="method-flexSetter" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-flexSetter" class="viewSource">view source</a></div><a name="flexSetter"></a><a name="method-flexSetter"></a><a href="Ext.Function.html#" rel="method-flexSetter" class="cls expand">flexSetter</a>(
190 <span class="pre">Function setter</span>)
191  : Function</div><div class="description"><div class="short">A very commonly used method throughout the framework. It acts as a wrapper around another method
192 which originally acc...</div><div class="long"><p>A very commonly used method throughout the framework. It acts as a wrapper around another method
193 which originally accepts 2 arguments for <code>name</code> and <code>value</code>.
194 The wrapped function then allows "flexible" value setting of either:</p>
195
196 <ul>
197      <li><code>name</code> and <code>value</code> as 2 arguments</li>
198      <li>one single object argument with multiple key - value pairs</li>
199 </ul>
200
201
202 <p>For example:</p>
203
204 <pre><code>var setValue = Ext.Function.flexSetter(function(name, value) {
205     this[name] = value;
206 });
207
208 // Afterwards
209 // Setting a single name - value
210 setValue('name1', 'value1');
211
212 // Settings multiple name - value pairs
213 setValue({
214     name1: 'value1',
215     name2: 'value2',
216     name3: 'value3'
217 });
218 </code></pre>
219
220 <h3 class="pa">Parameters</h3><ul><li><span class="pre">setter</span> : Function<div class="sub-desc">
221 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>flexSetter</p>
222 </li></ul></div></div></div><div id="method-pass" class="member ni"><a href="Ext.Function.html#method-pass" rel="method-pass" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.Function.html" class="definedIn docClass">Ext.Function</a><br/><a href="../source/Function.html#Ext-Function-method-pass" class="viewSource">view source</a></div><a name="pass"></a><a name="method-pass"></a><a href="Ext.Function.html#" rel="method-pass" class="cls expand">pass</a>(
223 <span class="pre">Function fn, Array args, [Object scope]</span>)
224  : Function</div><div class="description"><div class="short">Create a new function from the provided fn, the arguments of which are pre-set to args.
225 New arguments passed to the n...</div><div class="long"><p>Create a new function from the provided <code>fn</code>, the arguments of which are pre-set to <code>args</code>.
226 New arguments passed to the newly created callback when it's invoked are appended after the pre-set ones.
227 This is especially useful when creating callbacks.
228 For example:</p>
229
230 <pre><code>var originalFunction = function(){
231     alert(Ext.Array.from(arguments).join(' '));
232 };
233
234 var callback = Ext.Function.pass(originalFunction, ['Hello', 'World']);
235
236 callback(); // alerts 'Hello World'
237 callback('by Me'); // alerts 'Hello World by Me'
238 </code></pre>
239 <h3 class="pa">Parameters</h3><ul><li><span class="pre">fn</span> : Function<div class="sub-desc"><p>The original function</p>
240 </div></li><li><span class="pre">args</span> : Array<div class="sub-desc"><p>The arguments to pass to new callback</p>
241 </div></li><li><span class="pre">scope</span> : Object<div class="sub-desc"><p>(optional) The scope (<code><b>this</b></code> reference) in which the function is executed.</p>
242 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;<p>The new callback function</p>
243 </li></ul></div></div></div></div></div></div></div><div id="pageContent"></div></div></div></div></body></html>