/ext-4.1.0_b3/docs/source/Function2.html

https://bitbucket.org/srogerf/javascript · HTML · 502 lines · 467 code · 35 blank · 0 comment · 0 complexity · 65b6ce3947856a9ce3135de0d67b7bd8 MD5 · raw file

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-Function'>/**
  19. </span> * @class Ext.Function
  20. *
  21. * A collection of useful static methods to deal with function callbacks
  22. * @singleton
  23. * @alternateClassName Ext.util.Functions
  24. */
  25. Ext.Function = {
  26. <span id='Ext-Function-method-flexSetter'> /**
  27. </span> * A very commonly used method throughout the framework. It acts as a wrapper around another method
  28. * which originally accepts 2 arguments for `name` and `value`.
  29. * The wrapped function then allows &quot;flexible&quot; value setting of either:
  30. *
  31. * - `name` and `value` as 2 arguments
  32. * - one single object argument with multiple key - value pairs
  33. *
  34. * For example:
  35. *
  36. * var setValue = Ext.Function.flexSetter(function(name, value) {
  37. * this[name] = value;
  38. * });
  39. *
  40. * // Afterwards
  41. * // Setting a single name - value
  42. * setValue('name1', 'value1');
  43. *
  44. * // Settings multiple name - value pairs
  45. * setValue({
  46. * name1: 'value1',
  47. * name2: 'value2',
  48. * name3: 'value3'
  49. * });
  50. *
  51. * @param {Function} setter
  52. * @returns {Function} flexSetter
  53. */
  54. flexSetter: function(fn) {
  55. return function(a, b) {
  56. var k, i;
  57. if (a === null) {
  58. return this;
  59. }
  60. if (typeof a !== 'string') {
  61. for (k in a) {
  62. if (a.hasOwnProperty(k)) {
  63. fn.call(this, k, a[k]);
  64. }
  65. }
  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]);
  71. }
  72. }
  73. }
  74. } else {
  75. fn.call(this, a, b);
  76. }
  77. return this;
  78. };
  79. },
  80. <span id='Ext-Function-method-bind'> /**
  81. </span> * Create a new function from the provided `fn`, change `this` to the provided scope, optionally
  82. * overrides arguments for the call. (Defaults to the arguments passed by the caller)
  83. *
  84. * {@link Ext#bind Ext.bind} is alias for {@link Ext.Function#bind Ext.Function.bind}
  85. *
  86. * @param {Function} fn The function to delegate.
  87. * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed.
  88. * **If omitted, defaults to the default global environment object (usually the browser window).**
  89. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  90. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  91. * if a number the args are inserted at the specified position
  92. * @return {Function} The new function
  93. */
  94. bind: function(fn, scope, args, appendArgs) {
  95. if (arguments.length === 2) {
  96. return function() {
  97. return fn.apply(scope, arguments);
  98. };
  99. }
  100. var method = fn,
  101. slice = Array.prototype.slice;
  102. return function() {
  103. var callArgs = args || arguments;
  104. if (appendArgs === true) {
  105. callArgs = slice.call(arguments, 0);
  106. callArgs = callArgs.concat(args);
  107. }
  108. else if (typeof appendArgs == 'number') {
  109. callArgs = slice.call(arguments, 0); // copy arguments first
  110. Ext.Array.insert(callArgs, appendArgs, args);
  111. }
  112. return method.apply(scope || Ext.global, callArgs);
  113. };
  114. },
  115. <span id='Ext-Function-method-pass'> /**
  116. </span> * Create a new function from the provided `fn`, the arguments of which are pre-set to `args`.
  117. * New arguments passed to the newly created callback when it's invoked are appended after the pre-set ones.
  118. * This is especially useful when creating callbacks.
  119. *
  120. * For example:
  121. *
  122. * var originalFunction = function(){
  123. * alert(Ext.Array.from(arguments).join(' '));
  124. * };
  125. *
  126. * var callback = Ext.Function.pass(originalFunction, ['Hello', 'World']);
  127. *
  128. * callback(); // alerts 'Hello World'
  129. * callback('by Me'); // alerts 'Hello World by Me'
  130. *
  131. * {@link Ext#pass Ext.pass} is alias for {@link Ext.Function#pass Ext.Function.pass}
  132. *
  133. * @param {Function} fn The original function
  134. * @param {Array} args The arguments to pass to new callback
  135. * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed.
  136. * @return {Function} The new callback function
  137. */
  138. pass: function(fn, args, scope) {
  139. if (!Ext.isArray(args)) {
  140. if (Ext.isIterable(args)) {
  141. args = Ext.Array.clone(args);
  142. } else {
  143. args = args !== undefined ? [args] : [];
  144. }
  145. };
  146. return function() {
  147. var fnArgs = [].concat(args);
  148. fnArgs.push.apply(fnArgs, arguments);
  149. return fn.apply(scope || this, fnArgs);
  150. };
  151. },
  152. <span id='Ext-Function-method-alias'> /**
  153. </span> * Create an alias to the provided method property with name `methodName` of `object`.
  154. * Note that the execution scope will still be bound to the provided `object` itself.
  155. *
  156. * @param {Object/Function} object
  157. * @param {String} methodName
  158. * @return {Function} aliasFn
  159. */
  160. alias: function(object, methodName) {
  161. return function() {
  162. return object[methodName].apply(object, arguments);
  163. };
  164. },
  165. <span id='Ext-Function-method-clone'> /**
  166. </span> * Create a &quot;clone&quot; of the provided method. The returned method will call the given
  167. * method passing along all arguments and the &quot;this&quot; pointer and return its result.
  168. *
  169. * @param {Function} method
  170. * @return {Function} cloneFn
  171. */
  172. clone: function(method) {
  173. return function() {
  174. return method.apply(this, arguments);
  175. };
  176. },
  177. <span id='Ext-Function-method-createInterceptor'> /**
  178. </span> * Creates an interceptor function. The passed function is called before the original one. If it returns false,
  179. * the original one is not called. The resulting function returns the results of the original function.
  180. * The passed function is called with the parameters of the original function. Example usage:
  181. *
  182. * var sayHi = function(name){
  183. * alert('Hi, ' + name);
  184. * }
  185. *
  186. * sayHi('Fred'); // alerts &quot;Hi, Fred&quot;
  187. *
  188. * // create a new function that validates input without
  189. * // directly modifying the original function:
  190. * var sayHiToFriend = Ext.Function.createInterceptor(sayHi, function(name){
  191. * return name == 'Brian';
  192. * });
  193. *
  194. * sayHiToFriend('Fred'); // no alert
  195. * sayHiToFriend('Brian'); // alerts &quot;Hi, Brian&quot;
  196. *
  197. * @param {Function} origFn The original function.
  198. * @param {Function} newFn The function to call before the original
  199. * @param {Object} scope (optional) The scope (`this` reference) in which the passed function is executed.
  200. * **If omitted, defaults to the scope in which the original function is called or the browser window.**
  201. * @param {Object} returnValue (optional) The value to return if the passed function return false (defaults to null).
  202. * @return {Function} The new function
  203. */
  204. createInterceptor: function(origFn, newFn, scope, returnValue) {
  205. var method = origFn;
  206. if (!Ext.isFunction(newFn)) {
  207. return origFn;
  208. }
  209. else {
  210. return function() {
  211. var me = this,
  212. args = arguments;
  213. newFn.target = me;
  214. newFn.method = origFn;
  215. return (newFn.apply(scope || me || Ext.global, args) !== false) ? origFn.apply(me || Ext.global, args) : returnValue || null;
  216. };
  217. }
  218. },
  219. <span id='Ext-Function-method-createDelayed'> /**
  220. </span> * Creates a delegate (callback) which, when called, executes after a specific delay.
  221. *
  222. * @param {Function} fn The function which will be called on a delay when the returned function is called.
  223. * Optionally, a replacement (or additional) argument list may be specified.
  224. * @param {Number} delay The number of milliseconds to defer execution by whenever called.
  225. * @param {Object} scope (optional) The scope (`this` reference) used by the function at execution time.
  226. * @param {Array} args (optional) Override arguments for the call. (Defaults to the arguments passed by the caller)
  227. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  228. * if a number the args are inserted at the specified position.
  229. * @return {Function} A function which, when called, executes the original function after the specified delay.
  230. */
  231. createDelayed: function(fn, delay, scope, args, appendArgs) {
  232. if (scope || args) {
  233. fn = Ext.Function.bind(fn, scope, args, appendArgs);
  234. }
  235. return function() {
  236. var me = this,
  237. args = Array.prototype.slice.call(arguments);
  238. setTimeout(function() {
  239. fn.apply(me, args);
  240. }, delay);
  241. };
  242. },
  243. <span id='Ext-Function-method-defer'> /**
  244. </span> * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
  245. *
  246. * var sayHi = function(name){
  247. * alert('Hi, ' + name);
  248. * }
  249. *
  250. * // executes immediately:
  251. * sayHi('Fred');
  252. *
  253. * // executes after 2 seconds:
  254. * Ext.Function.defer(sayHi, 2000, this, ['Fred']);
  255. *
  256. * // this syntax is sometimes useful for deferring
  257. * // execution of an anonymous function:
  258. * Ext.Function.defer(function(){
  259. * alert('Anonymous');
  260. * }, 100);
  261. *
  262. * {@link Ext#defer Ext.defer} is alias for {@link Ext.Function#defer Ext.Function.defer}
  263. *
  264. * @param {Function} fn The function to defer.
  265. * @param {Number} millis The number of milliseconds for the setTimeout call
  266. * (if less than or equal to 0 the function is executed immediately)
  267. * @param {Object} scope (optional) The scope (`this` reference) in which the function is executed.
  268. * **If omitted, defaults to the browser window.**
  269. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  270. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  271. * if a number the args are inserted at the specified position
  272. * @return {Number} The timeout id that can be used with clearTimeout
  273. */
  274. defer: function(fn, millis, scope, args, appendArgs) {
  275. fn = Ext.Function.bind(fn, scope, args, appendArgs);
  276. if (millis &gt; 0) {
  277. return setTimeout(fn, millis);
  278. }
  279. fn();
  280. return 0;
  281. },
  282. <span id='Ext-Function-method-createSequence'> /**
  283. </span> * Create a combined function call sequence of the original function + the passed function.
  284. * The resulting function returns the results of the original function.
  285. * The passed function is called with the parameters of the original function. Example usage:
  286. *
  287. * var sayHi = function(name){
  288. * alert('Hi, ' + name);
  289. * }
  290. *
  291. * sayHi('Fred'); // alerts &quot;Hi, Fred&quot;
  292. *
  293. * var sayGoodbye = Ext.Function.createSequence(sayHi, function(name){
  294. * alert('Bye, ' + name);
  295. * });
  296. *
  297. * sayGoodbye('Fred'); // both alerts show
  298. *
  299. * @param {Function} originalFn The original function.
  300. * @param {Function} newFn The function to sequence
  301. * @param {Object} scope (optional) The scope (`this` reference) in which the passed function is executed.
  302. * If omitted, defaults to the scope in which the original function is called or the default global environment object (usually the browser window).
  303. * @return {Function} The new function
  304. */
  305. createSequence: function(originalFn, newFn, scope) {
  306. if (!newFn) {
  307. return originalFn;
  308. }
  309. else {
  310. return function() {
  311. var result = originalFn.apply(this, arguments);
  312. newFn.apply(scope || this, arguments);
  313. return result;
  314. };
  315. }
  316. },
  317. <span id='Ext-Function-method-createBuffered'> /**
  318. </span> * Creates a delegate function, optionally with a bound scope which, when called, buffers
  319. * the execution of the passed function for the configured number of milliseconds.
  320. * If called again within that period, the impending invocation will be canceled, and the
  321. * timeout period will begin again.
  322. *
  323. * @param {Function} fn The function to invoke on a buffered timer.
  324. * @param {Number} buffer The number of milliseconds by which to buffer the invocation of the
  325. * function.
  326. * @param {Object} scope (optional) The scope (`this` reference) in which
  327. * the passed function is executed. If omitted, defaults to the scope specified by the caller.
  328. * @param {Array} args (optional) Override arguments for the call. Defaults to the arguments
  329. * passed by the caller.
  330. * @return {Function} A function which invokes the passed function after buffering for the specified time.
  331. */
  332. createBuffered: function(fn, buffer, scope, args) {
  333. var timerId;
  334. return function() {
  335. var callArgs = args || Array.prototype.slice.call(arguments, 0),
  336. me = scope || this;
  337. if (timerId) {
  338. clearTimeout(timerId);
  339. }
  340. timerId = setTimeout(function(){
  341. fn.apply(me, callArgs);
  342. }, buffer);
  343. };
  344. },
  345. <span id='Ext-Function-method-createThrottled'> /**
  346. </span> * Creates a throttled version of the passed function which, when called repeatedly and
  347. * rapidly, invokes the passed function only after a certain interval has elapsed since the
  348. * previous invocation.
  349. *
  350. * This is useful for wrapping functions which may be called repeatedly, such as
  351. * a handler of a mouse move event when the processing is expensive.
  352. *
  353. * @param {Function} fn The function to execute at a regular time interval.
  354. * @param {Number} interval The interval **in milliseconds** on which the passed function is executed.
  355. * @param {Object} scope (optional) The scope (`this` reference) in which
  356. * the passed function is executed. If omitted, defaults to the scope specified by the caller.
  357. * @returns {Function} A function which invokes the passed function at the specified interval.
  358. */
  359. createThrottled: function(fn, interval, scope) {
  360. var lastCallTime, elapsed, lastArgs, timer, execute = function() {
  361. fn.apply(scope || this, lastArgs);
  362. lastCallTime = new Date().getTime();
  363. };
  364. return function() {
  365. elapsed = new Date().getTime() - lastCallTime;
  366. lastArgs = arguments;
  367. clearTimeout(timer);
  368. if (!lastCallTime || (elapsed &gt;= interval)) {
  369. execute();
  370. } else {
  371. timer = setTimeout(execute, interval - elapsed);
  372. }
  373. };
  374. },
  375. <span id='Ext-Function-method-interceptBefore'> /**
  376. </span> * Adds behavior to an existing method that is executed before the
  377. * original behavior of the function. For example:
  378. *
  379. * var soup = {
  380. * contents: [],
  381. * add: function(ingredient) {
  382. * this.contents.push(ingredient);
  383. * }
  384. * };
  385. * Ext.Function.interceptBefore(soup, &quot;add&quot;, function(ingredient){
  386. * if (!this.contents.length &amp;&amp; ingredient !== &quot;water&quot;) {
  387. * // Always add water to start with
  388. * this.contents.push(&quot;water&quot;);
  389. * }
  390. * });
  391. * soup.add(&quot;onions&quot;);
  392. * soup.add(&quot;salt&quot;);
  393. * soup.contents; // will contain: water, onions, salt
  394. *
  395. * @param {Object} object The target object
  396. * @param {String} methodName Name of the method to override
  397. * @param {Function} fn Function with the new behavior. It will
  398. * be called with the same arguments as the original method. The
  399. * return value of this function will be the return value of the
  400. * new method.
  401. * @param {Object} [scope] The scope to execute the interceptor function. Defaults to the object.
  402. * @return {Function} The new function just created.
  403. */
  404. interceptBefore: function(object, methodName, fn, scope) {
  405. var method = object[methodName] || Ext.emptyFn;
  406. return (object[methodName] = function() {
  407. var ret = fn.apply(scope || this, arguments);
  408. method.apply(this, arguments);
  409. return ret;
  410. });
  411. },
  412. <span id='Ext-Function-method-interceptAfter'> /**
  413. </span> * Adds behavior to an existing method that is executed after the
  414. * original behavior of the function. For example:
  415. *
  416. * var soup = {
  417. * contents: [],
  418. * add: function(ingredient) {
  419. * this.contents.push(ingredient);
  420. * }
  421. * };
  422. * Ext.Function.interceptAfter(soup, &quot;add&quot;, function(ingredient){
  423. * // Always add a bit of extra salt
  424. * this.contents.push(&quot;salt&quot;);
  425. * });
  426. * soup.add(&quot;water&quot;);
  427. * soup.add(&quot;onions&quot;);
  428. * soup.contents; // will contain: water, salt, onions, salt
  429. *
  430. * @param {Object} object The target object
  431. * @param {String} methodName Name of the method to override
  432. * @param {Function} fn Function with the new behavior. It will
  433. * be called with the same arguments as the original method. The
  434. * return value of this function will be the return value of the
  435. * new method.
  436. * @param {Object} [scope] The scope to execute the interceptor function. Defaults to the object.
  437. * @return {Function} The new function just created.
  438. */
  439. interceptAfter: function(object, methodName, fn, scope) {
  440. var method = object[methodName] || Ext.emptyFn;
  441. return (object[methodName] = function() {
  442. method.apply(this, arguments);
  443. return fn.apply(scope || this, arguments);
  444. });
  445. }
  446. };
  447. <span id='Ext-method-defer'>/**
  448. </span> * @method
  449. * @member Ext
  450. * @inheritdoc Ext.Function#defer
  451. */
  452. Ext.defer = Ext.Function.alias(Ext.Function, 'defer');
  453. <span id='Ext-method-pass'>/**
  454. </span> * @method
  455. * @member Ext
  456. * @inheritdoc Ext.Function#pass
  457. */
  458. Ext.pass = Ext.Function.alias(Ext.Function, 'pass');
  459. <span id='Ext-method-bind'>/**
  460. </span> * @method
  461. * @member Ext
  462. * @inheritdoc Ext.Function#bind
  463. */
  464. Ext.bind = Ext.Function.alias(Ext.Function, 'bind');
  465. </pre>
  466. </body>
  467. </html>