Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Class.html
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="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../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-Class'>/**
19 </span> * @author Jacky Nguyen &lt;jacky@sencha.com&gt;
20  * @docauthor Jacky Nguyen &lt;jacky@sencha.com&gt;
21  * @class Ext.Class
22  * 
23  * Handles class creation throughout the whole framework. Note that most of the time {@link Ext#define Ext.define} should
24  * be used instead, since it's a higher level wrapper that aliases to {@link Ext.ClassManager#create}
25  * to enable namespacing and dynamic dependency resolution.
26  * 
27  * # Basic syntax: #
28  * 
29  *     Ext.define(className, properties);
30  * 
31  * in which `properties` is an object represent a collection of properties that apply to the class. See
32  * {@link Ext.ClassManager#create} for more detailed instructions.
33  * 
34  *     Ext.define('Person', {
35  *          name: 'Unknown',
36  * 
37  *          constructor: function(name) {
38  *              if (name) {
39  *                  this.name = name;
40  *              }
41  * 
42  *              return this;
43  *          },
44  * 
45  *          eat: function(foodType) {
46  *              alert(&quot;I'm eating: &quot; + foodType);
47  * 
48  *              return this;
49  *          }
50  *     });
51  * 
52  *     var aaron = new Person(&quot;Aaron&quot;);
53  *     aaron.eat(&quot;Sandwich&quot;); // alert(&quot;I'm eating: Sandwich&quot;);
54  * 
55  * Ext.Class has a powerful set of extensible {@link Ext.Class#registerPreprocessor pre-processors} which takes care of
56  * everything related to class creation, including but not limited to inheritance, mixins, configuration, statics, etc.
57  * 
58  * # Inheritance: #
59  * 
60  *     Ext.define('Developer', {
61  *          extend: 'Person',
62  * 
63  *          constructor: function(name, isGeek) {
64  *              this.isGeek = isGeek;
65  * 
66  *              // Apply a method from the parent class' prototype
67  *              this.callParent([name]);
68  * 
69  *              return this;
70  * 
71  *          },
72  * 
73  *          code: function(language) {
74  *              alert(&quot;I'm coding in: &quot; + language);
75  * 
76  *              this.eat(&quot;Bugs&quot;);
77  * 
78  *              return this;
79  *          }
80  *     });
81  * 
82  *     var jacky = new Developer(&quot;Jacky&quot;, true);
83  *     jacky.code(&quot;JavaScript&quot;); // alert(&quot;I'm coding in: JavaScript&quot;);
84  *                               // alert(&quot;I'm eating: Bugs&quot;);
85  * 
86  * See {@link Ext.Base#callParent} for more details on calling superclass' methods
87  * 
88  * # Mixins: #
89  * 
90  *     Ext.define('CanPlayGuitar', {
91  *          playGuitar: function() {
92  *             alert(&quot;F#...G...D...A&quot;);
93  *          }
94  *     });
95  * 
96  *     Ext.define('CanComposeSongs', {
97  *          composeSongs: function() { ... }
98  *     });
99  * 
100  *     Ext.define('CanSing', {
101  *          sing: function() {
102  *              alert(&quot;I'm on the highway to hell...&quot;)
103  *          }
104  *     });
105  * 
106  *     Ext.define('Musician', {
107  *          extend: 'Person',
108  * 
109  *          mixins: {
110  *              canPlayGuitar: 'CanPlayGuitar',
111  *              canComposeSongs: 'CanComposeSongs',
112  *              canSing: 'CanSing'
113  *          }
114  *     })
115  * 
116  *     Ext.define('CoolPerson', {
117  *          extend: 'Person',
118  * 
119  *          mixins: {
120  *              canPlayGuitar: 'CanPlayGuitar',
121  *              canSing: 'CanSing'
122  *          },
123  * 
124  *          sing: function() {
125  *              alert(&quot;Ahem....&quot;);
126  * 
127  *              this.mixins.canSing.sing.call(this);
128  * 
129  *              alert(&quot;[Playing guitar at the same time...]&quot;);
130  * 
131  *              this.playGuitar();
132  *          }
133  *     });
134  * 
135  *     var me = new CoolPerson(&quot;Jacky&quot;);
136  * 
137  *     me.sing(); // alert(&quot;Ahem...&quot;);
138  *                // alert(&quot;I'm on the highway to hell...&quot;);
139  *                // alert(&quot;[Playing guitar at the same time...]&quot;);
140  *                // alert(&quot;F#...G...D...A&quot;);
141  * 
142  * # Config: #
143  * 
144  *     Ext.define('SmartPhone', {
145  *          config: {
146  *              hasTouchScreen: false,
147  *              operatingSystem: 'Other',
148  *              price: 500
149  *          },
150  * 
151  *          isExpensive: false,
152  * 
153  *          constructor: function(config) {
154  *              this.initConfig(config);
155  * 
156  *              return this;
157  *          },
158  * 
159  *          applyPrice: function(price) {
160  *              this.isExpensive = (price &gt; 500);
161  * 
162  *              return price;
163  *          },
164  * 
165  *          applyOperatingSystem: function(operatingSystem) {
166  *              if (!(/^(iOS|Android|BlackBerry)$/i).test(operatingSystem)) {
167  *                  return 'Other';
168  *              }
169  * 
170  *              return operatingSystem;
171  *          }
172  *     });
173  * 
174  *     var iPhone = new SmartPhone({
175  *          hasTouchScreen: true,
176  *          operatingSystem: 'iOS'
177  *     });
178  * 
179  *     iPhone.getPrice(); // 500;
180  *     iPhone.getOperatingSystem(); // 'iOS'
181  *     iPhone.getHasTouchScreen(); // true;
182  *     iPhone.hasTouchScreen(); // true
183  * 
184  *     iPhone.isExpensive; // false;
185  *     iPhone.setPrice(600);
186  *     iPhone.getPrice(); // 600
187  *     iPhone.isExpensive; // true;
188  * 
189  *     iPhone.setOperatingSystem('AlienOS');
190  *     iPhone.getOperatingSystem(); // 'Other'
191  * 
192  * # Statics: #
193  * 
194  *     Ext.define('Computer', {
195  *          statics: {
196  *              factory: function(brand) {
197  *                 // 'this' in static methods refer to the class itself
198  *                  return new this(brand);
199  *              }
200  *          },
201  * 
202  *          constructor: function() { ... }
203  *     });
204  * 
205  *     var dellComputer = Computer.factory('Dell');
206  * 
207  * Also see {@link Ext.Base#statics} and {@link Ext.Base#self} for more details on accessing
208  * static properties within class methods
209  *
210  */
211 (function() {
212
213     var Class,
214         Base = Ext.Base,
215         baseStaticProperties = [],
216         baseStaticProperty;
217
218     for (baseStaticProperty in Base) {
219         if (Base.hasOwnProperty(baseStaticProperty)) {
220             baseStaticProperties.push(baseStaticProperty);
221         }
222     }
223
224 <span id='Ext-Class-method-constructor'><span id='Ext-Class'>    /**
225 </span></span>     * @constructor
226      * @param {Object} classData An object represent the properties of this class
227      * @param {Function} createdFn Optional, the callback function to be executed when this class is fully created.
228      * Note that the creation process can be asynchronous depending on the pre-processors used.
229      * @return {Ext.Base} The newly created class
230      */
231     Ext.Class = Class = function(newClass, classData, onClassCreated) {
232         if (typeof newClass !== 'function') {
233             onClassCreated = classData;
234             classData = newClass;
235             newClass = function() {
236                 return this.constructor.apply(this, arguments);
237             };
238         }
239
240         if (!classData) {
241             classData = {};
242         }
243
244         var preprocessorStack = classData.preprocessors || Class.getDefaultPreprocessors(),
245             registeredPreprocessors = Class.getPreprocessors(),
246             index = 0,
247             preprocessors = [],
248             preprocessor, preprocessors, staticPropertyName, process, i, j, ln;
249
250         for (i = 0, ln = baseStaticProperties.length; i &lt; ln; i++) {
251             staticPropertyName = baseStaticProperties[i];
252             newClass[staticPropertyName] = Base[staticPropertyName];
253         }
254
255         delete classData.preprocessors;
256
257         for (j = 0, ln = preprocessorStack.length; j &lt; ln; j++) {
258             preprocessor = preprocessorStack[j];
259
260             if (typeof preprocessor === 'string') {
261                 preprocessor = registeredPreprocessors[preprocessor];
262
263                 if (!preprocessor.always) {
264                     if (classData.hasOwnProperty(preprocessor.name)) {
265                         preprocessors.push(preprocessor.fn);
266                     }
267                 }
268                 else {
269                     preprocessors.push(preprocessor.fn);
270                 }
271             }
272             else {
273                 preprocessors.push(preprocessor);
274             }
275         }
276
277         classData.onClassCreated = onClassCreated;
278
279         classData.onBeforeClassCreated = function(cls, data) {
280             onClassCreated = data.onClassCreated;
281
282             delete data.onBeforeClassCreated;
283             delete data.onClassCreated;
284
285             cls.implement(data);
286
287             if (onClassCreated) {
288                 onClassCreated.call(cls, cls);
289             }
290         };
291
292         process = function(cls, data) {
293             preprocessor = preprocessors[index++];
294
295             if (!preprocessor) {
296                 data.onBeforeClassCreated.apply(this, arguments);
297                 return;
298             }
299
300             if (preprocessor.call(this, cls, data, process) !== false) {
301                 process.apply(this, arguments);
302             }
303         };
304
305         process.call(Class, newClass, classData);
306
307         return newClass;
308     };
309
310     Ext.apply(Class, {
311
312 <span id='Ext-Class-property-preprocessors'>        /** @private */
313 </span>        preprocessors: {},
314
315 <span id='Ext-Class-method-registerPreprocessor'>        /**
316 </span>         * Register a new pre-processor to be used during the class creation process
317          *
318          * @member Ext.Class registerPreprocessor
319          * @param {String} name The pre-processor's name
320          * @param {Function} fn The callback function to be executed. Typical format:
321
322     function(cls, data, fn) {
323         // Your code here
324
325         // Execute this when the processing is finished.
326         // Asynchronous processing is perfectly ok
327         if (fn) {
328             fn.call(this, cls, data);
329         }
330     });
331
332          * Passed arguments for this function are:
333          *
334          * - `{Function} cls`: The created class
335          * - `{Object} data`: The set of properties passed in {@link Ext.Class} constructor
336          * - `{Function} fn`: The callback function that &lt;b&gt;must&lt;/b&gt; to be executed when this pre-processor finishes,
337          * regardless of whether the processing is synchronous or aynchronous
338          *
339          * @return {Ext.Class} this
340          * @markdown
341          */
342         registerPreprocessor: function(name, fn, always) {
343             this.preprocessors[name] = {
344                 name: name,
345                 always: always ||  false,
346                 fn: fn
347             };
348
349             return this;
350         },
351
352 <span id='Ext-Class-method-getPreprocessor'>        /**
353 </span>         * Retrieve a pre-processor callback function by its name, which has been registered before
354          *
355          * @param {String} name
356          * @return {Function} preprocessor
357          */
358         getPreprocessor: function(name) {
359             return this.preprocessors[name];
360         },
361
362         getPreprocessors: function() {
363             return this.preprocessors;
364         },
365
366 <span id='Ext-Class-method-getDefaultPreprocessors'>        /**
367 </span>         * Retrieve the array stack of default pre-processors
368          *
369          * @return {Function} defaultPreprocessors
370          */
371         getDefaultPreprocessors: function() {
372             return this.defaultPreprocessors || [];
373         },
374
375 <span id='Ext-Class-method-setDefaultPreprocessors'>        /**
376 </span>         * Set the default array stack of default pre-processors
377          *
378          * @param {Array} preprocessors
379          * @return {Ext.Class} this
380          */
381         setDefaultPreprocessors: function(preprocessors) {
382             this.defaultPreprocessors = Ext.Array.from(preprocessors);
383
384             return this;
385         },
386
387 <span id='Ext-Class-method-setDefaultPreprocessorPosition'>        /**
388 </span>         * Insert this pre-processor at a specific position in the stack, optionally relative to
389          * any existing pre-processor. For example:
390
391     Ext.Class.registerPreprocessor('debug', function(cls, data, fn) {
392         // Your code here
393
394         if (fn) {
395             fn.call(this, cls, data);
396         }
397     }).insertDefaultPreprocessor('debug', 'last');
398
399          * @param {String} name The pre-processor name. Note that it needs to be registered with
400          * {@link Ext#registerPreprocessor registerPreprocessor} before this
401          * @param {String} offset The insertion position. Four possible values are:
402          * 'first', 'last', or: 'before', 'after' (relative to the name provided in the third argument)
403          * @param {String} relativeName
404          * @return {Ext.Class} this
405          * @markdown
406          */
407         setDefaultPreprocessorPosition: function(name, offset, relativeName) {
408             var defaultPreprocessors = this.defaultPreprocessors,
409                 index;
410
411             if (typeof offset === 'string') {
412                 if (offset === 'first') {
413                     defaultPreprocessors.unshift(name);
414
415                     return this;
416                 }
417                 else if (offset === 'last') {
418                     defaultPreprocessors.push(name);
419
420                     return this;
421                 }
422
423                 offset = (offset === 'after') ? 1 : -1;
424             }
425
426             index = Ext.Array.indexOf(defaultPreprocessors, relativeName);
427
428             if (index !== -1) {
429                 defaultPreprocessors.splice(Math.max(0, index + offset), 0, name);
430             }
431
432             return this;
433         }
434     });
435
436     Class.registerPreprocessor('extend', function(cls, data) {
437         var extend = data.extend,
438             base = Ext.Base,
439             basePrototype = base.prototype,
440             prototype = function() {},
441             parent, i, k, ln, staticName, parentStatics,
442             parentPrototype, clsPrototype;
443
444         if (extend &amp;&amp; extend !== Object) {
445             parent = extend;
446         }
447         else {
448             parent = base;
449         }
450
451         parentPrototype = parent.prototype;
452
453         prototype.prototype = parentPrototype;
454         clsPrototype = cls.prototype = new prototype();
455
456         if (!('$class' in parent)) {
457             for (i in basePrototype) {
458                 if (!parentPrototype[i]) {
459                     parentPrototype[i] = basePrototype[i];
460                 }
461             }
462         }
463
464         clsPrototype.self = cls;
465
466         cls.superclass = clsPrototype.superclass = parentPrototype;
467
468         delete data.extend;
469
470         // Statics inheritance
471         parentStatics = parentPrototype.$inheritableStatics;
472
473         if (parentStatics) {
474             for (k = 0, ln = parentStatics.length; k &lt; ln; k++) {
475                 staticName = parentStatics[k];
476
477                 if (!cls.hasOwnProperty(staticName)) {
478                     cls[staticName] = parent[staticName];
479                 }
480             }
481         }
482
483         // Merge the parent class' config object without referencing it
484         if (parentPrototype.config) {
485             clsPrototype.config = Ext.Object.merge({}, parentPrototype.config);
486         }
487         else {
488             clsPrototype.config = {};
489         }
490
491         if (clsPrototype.$onExtended) {
492             clsPrototype.$onExtended.call(cls, cls, data);
493         }
494
495         if (data.onClassExtended) {
496             clsPrototype.$onExtended = data.onClassExtended;
497             delete data.onClassExtended;
498         }
499
500     }, true);
501
502     Class.registerPreprocessor('statics', function(cls, data) {
503         var statics = data.statics,
504             name;
505
506         for (name in statics) {
507             if (statics.hasOwnProperty(name)) {
508                 cls[name] = statics[name];
509             }
510         }
511
512         delete data.statics;
513     });
514
515     Class.registerPreprocessor('inheritableStatics', function(cls, data) {
516         var statics = data.inheritableStatics,
517             inheritableStatics,
518             prototype = cls.prototype,
519             name;
520
521         inheritableStatics = prototype.$inheritableStatics;
522
523         if (!inheritableStatics) {
524             inheritableStatics = prototype.$inheritableStatics = [];
525         }
526
527         for (name in statics) {
528             if (statics.hasOwnProperty(name)) {
529                 cls[name] = statics[name];
530                 inheritableStatics.push(name);
531             }
532         }
533
534         delete data.inheritableStatics;
535     });
536
537     Class.registerPreprocessor('mixins', function(cls, data) {
538         cls.mixin(data.mixins);
539
540         delete data.mixins;
541     });
542
543     Class.registerPreprocessor('config', function(cls, data) {
544         var prototype = cls.prototype;
545
546         Ext.Object.each(data.config, function(name) {
547             var cName = name.charAt(0).toUpperCase() + name.substr(1),
548                 pName = name,
549                 apply = 'apply' + cName,
550                 setter = 'set' + cName,
551                 getter = 'get' + cName;
552
553             if (!(apply in prototype) &amp;&amp; !data.hasOwnProperty(apply)) {
554                 data[apply] = function(val) {
555                     return val;
556                 };
557             }
558
559             if (!(setter in prototype) &amp;&amp; !data.hasOwnProperty(setter)) {
560                 data[setter] = function(val) {
561                     var ret = this[apply].call(this, val, this[pName]);
562
563                     if (ret !== undefined) {
564                         this[pName] = ret;
565                     }
566
567                     return this;
568                 };
569             }
570
571             if (!(getter in prototype) &amp;&amp; !data.hasOwnProperty(getter)) {
572                 data[getter] = function() {
573                     return this[pName];
574                 };
575             }
576         });
577
578         Ext.Object.merge(prototype.config, data.config);
579         delete data.config;
580     });
581
582     Class.setDefaultPreprocessors(['extend', 'statics', 'inheritableStatics', 'mixins', 'config']);
583
584     // Backwards compatible
585     Ext.extend = function(subclass, superclass, members) {
586         if (arguments.length === 2 &amp;&amp; Ext.isObject(superclass)) {
587             members = superclass;
588             superclass = subclass;
589             subclass = null;
590         }
591
592         var cls;
593
594         if (!superclass) {
595             Ext.Error.raise(&quot;Attempting to extend from a class which has not been loaded on the page.&quot;);
596         }
597
598         members.extend = superclass;
599         members.preprocessors = ['extend', 'mixins', 'config', 'statics'];
600
601         if (subclass) {
602             cls = new Class(subclass, members);
603         }
604         else {
605             cls = new Class(members);
606         }
607
608         cls.prototype.override = function(o) {
609             for (var m in o) {
610                 if (o.hasOwnProperty(m)) {
611                     this[m] = o[m];
612                 }
613             }
614         };
615
616         return cls;
617     };
618
619 })();
620 </pre>
621 </body>
622 </html>