]> git.proxmox.com Git - sencha-touch.git/blob - src/src/core/class/Base.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / core / class / Base.js
1 //@tag foundation,core
2 //@define Ext.Base
3 //@require Ext.Date
4
5 /**
6 * @class Ext.Base
7 * @author Jacky Nguyen <jacky@sencha.com>
8 *
9 * The root of all classes created with {@link Ext#define}.
10 *
11 * Ext.Base is the building block of all Ext classes. All classes in Ext inherit from Ext.Base. All prototype and static
12 * members of this class are inherited by all other classes.
13 *
14 * See the [Class System Guide](../../../core_concepts/class_system.html) for more.
15 *
16 */
17 (function(flexSetter) {
18
19 var noArgs = [],
20 Base = function(){};
21
22 // These static properties will be copied to every newly created class with {@link Ext#define}
23 Ext.apply(Base, {
24 $className: 'Ext.Base',
25
26 $isClass: true,
27
28 /**
29 * Create a new instance of this Class.
30 *
31 * Ext.define('My.cool.Class', {
32 * // ...
33 * });
34 *
35 * My.cool.Class.create({
36 * someConfig: true
37 * });
38 *
39 * All parameters are passed to the constructor of the class.
40 *
41 * @return {Object} the created instance.
42 * @static
43 * @inheritable
44 */
45 create: function() {
46 return Ext.create.apply(Ext, [this].concat(Array.prototype.slice.call(arguments, 0)));
47 },
48
49 /**
50 * @private
51 * @static
52 * @inheritable
53 */
54 extend: function(parent) {
55 var parentPrototype = parent.prototype,
56 prototype, i, ln, name, statics;
57
58 prototype = this.prototype = Ext.Object.chain(parentPrototype);
59 prototype.self = this;
60
61 this.superclass = prototype.superclass = parentPrototype;
62
63 if (!parent.$isClass) {
64 Ext.apply(prototype, Ext.Base.prototype);
65 prototype.constructor = function() {
66 parentPrototype.constructor.apply(this, arguments);
67 };
68 }
69
70 //<feature classSystem.inheritableStatics>
71 // Statics inheritance
72 statics = parentPrototype.$inheritableStatics;
73
74 if (statics) {
75 for (i = 0,ln = statics.length; i < ln; i++) {
76 name = statics[i];
77
78 if (!this.hasOwnProperty(name)) {
79 this[name] = parent[name];
80 }
81 }
82 }
83 //</feature>
84
85 if (parent.$onExtended) {
86 this.$onExtended = parent.$onExtended.slice();
87 }
88
89 //<feature classSystem.config>
90 prototype.config = prototype.defaultConfig = new prototype.configClass;
91 prototype.initConfigList = prototype.initConfigList.slice();
92 prototype.initConfigMap = Ext.Object.chain(prototype.initConfigMap);
93 //</feature>
94 },
95
96 /**
97 * @private
98 * @static
99 * @inheritable
100 */
101 '$onExtended': [],
102
103 /**
104 * @private
105 * @static
106 * @inheritable
107 */
108 triggerExtended: function() {
109 var callbacks = this.$onExtended,
110 ln = callbacks.length,
111 i, callback;
112
113 if (ln > 0) {
114 for (i = 0; i < ln; i++) {
115 callback = callbacks[i];
116 callback.fn.apply(callback.scope || this, arguments);
117 }
118 }
119 },
120
121 /**
122 * @private
123 * @static
124 * @inheritable
125 */
126 onExtended: function(fn, scope) {
127 this.$onExtended.push({
128 fn: fn,
129 scope: scope
130 });
131
132 return this;
133 },
134
135 /**
136 * @private
137 * @static
138 * @inheritable
139 */
140 addConfig: function(config, fullMerge) {
141 var prototype = this.prototype,
142 initConfigList = prototype.initConfigList,
143 initConfigMap = prototype.initConfigMap,
144 defaultConfig = prototype.defaultConfig,
145 hasInitConfigItem, name, value;
146
147 fullMerge = Boolean(fullMerge);
148
149 for (name in config) {
150 if (config.hasOwnProperty(name) && (fullMerge || !(name in defaultConfig))) {
151 value = config[name];
152 hasInitConfigItem = initConfigMap[name];
153
154 if (value !== null) {
155 if (!hasInitConfigItem) {
156 initConfigMap[name] = true;
157 initConfigList.push(name);
158 }
159 }
160 else if (hasInitConfigItem) {
161 initConfigMap[name] = false;
162 Ext.Array.remove(initConfigList, name);
163 }
164 }
165 }
166
167 if (fullMerge) {
168 Ext.merge(defaultConfig, config);
169 }
170 else {
171 Ext.mergeIf(defaultConfig, config);
172 }
173
174 prototype.configClass = Ext.Object.classify(defaultConfig);
175 },
176
177 /**
178 * Add / override static properties of this class.
179 *
180 * Ext.define('My.cool.Class', {
181 * // this.se
182 * });
183 *
184 * My.cool.Class.addStatics({
185 * someProperty: 'someValue', // My.cool.Class.someProperty = 'someValue'
186 * method1: function() { }, // My.cool.Class.method1 = function() { ... };
187 * method2: function() { } // My.cool.Class.method2 = function() { ... };
188 * });
189 *
190 * @param {Object} members
191 * @return {Ext.Base} this
192 * @static
193 * @inheritable
194 */
195 addStatics: function(members) {
196 var member, name;
197 //<debug>
198 var className = Ext.getClassName(this);
199 //</debug>
200
201 for (name in members) {
202 if (members.hasOwnProperty(name)) {
203 member = members[name];
204 //<debug>
205 if (typeof member == 'function') {
206 member.displayName = className + '.' + name;
207 }
208 //</debug>
209 this[name] = member;
210 }
211 }
212
213 return this;
214 },
215
216 /**
217 * @private
218 * @static
219 * @inheritable
220 */
221 addInheritableStatics: function(members) {
222 var inheritableStatics,
223 hasInheritableStatics,
224 prototype = this.prototype,
225 name, member;
226
227 inheritableStatics = prototype.$inheritableStatics;
228 hasInheritableStatics = prototype.$hasInheritableStatics;
229
230 if (!inheritableStatics) {
231 inheritableStatics = prototype.$inheritableStatics = [];
232 hasInheritableStatics = prototype.$hasInheritableStatics = {};
233 }
234
235 //<debug>
236 var className = Ext.getClassName(this);
237 //</debug>
238
239 for (name in members) {
240 if (members.hasOwnProperty(name)) {
241 member = members[name];
242 //<debug>
243 if (typeof member == 'function') {
244 member.displayName = className + '.' + name;
245 }
246 //</debug>
247 this[name] = member;
248
249 if (!hasInheritableStatics[name]) {
250 hasInheritableStatics[name] = true;
251 inheritableStatics.push(name);
252 }
253 }
254 }
255
256 return this;
257 },
258
259 /**
260 * Add methods / properties to the prototype of this class.
261 *
262 * @example
263 * Ext.define('My.awesome.Cat', {
264 * constructor: function() {
265 * // ...
266 * }
267 * });
268 *
269 * My.awesome.Cat.addMembers({
270 * meow: function() {
271 * alert('Meowww...');
272 * }
273 * });
274 *
275 * var kitty = new My.awesome.Cat();
276 * kitty.meow();
277 *
278 * @param {Object} members
279 * @static
280 * @inheritable
281 */
282 addMembers: function(members) {
283 var prototype = this.prototype,
284 names = [],
285 name, member;
286
287 //<debug>
288 var className = this.$className || '';
289 //</debug>
290
291 for (name in members) {
292 if (members.hasOwnProperty(name)) {
293 member = members[name];
294
295 if (typeof member == 'function' && !member.$isClass && member !== Ext.emptyFn) {
296 member.$owner = this;
297 member.$name = name;
298 //<debug>
299 member.displayName = className + '#' + name;
300 //</debug>
301 }
302
303 prototype[name] = member;
304 }
305 }
306
307 return this;
308 },
309
310 /**
311 * @private
312 * @static
313 * @inheritable
314 */
315 addMember: function(name, member) {
316 if (typeof member == 'function' && !member.$isClass && member !== Ext.emptyFn) {
317 member.$owner = this;
318 member.$name = name;
319 //<debug>
320 member.displayName = (this.$className || '') + '#' + name;
321 //</debug>
322 }
323
324 this.prototype[name] = member;
325
326 return this;
327 },
328
329 /**
330 * @private
331 * @static
332 * @inheritable
333 */
334 implement: function() {
335 this.addMembers.apply(this, arguments);
336 },
337
338 /**
339 * Borrow another class' members to the prototype of this class.
340 *
341 * Ext.define('Bank', {
342 * money: '$$$',
343 * printMoney: function() {
344 * alert('$$$$$$$');
345 * }
346 * });
347 *
348 * Ext.define('Thief', {
349 * // ...
350 * });
351 *
352 * Thief.borrow(Bank, ['money', 'printMoney']);
353 *
354 * var steve = new Thief();
355 *
356 * alert(steve.money); // alerts '$$$'
357 * steve.printMoney(); // alerts '$$$$$$$'
358 *
359 * @param {Ext.Base} fromClass The class to borrow members from
360 * @param {Array/String} members The names of the members to borrow
361 * @return {Ext.Base} this
362 * @static
363 * @inheritable
364 * @private
365 */
366 borrow: function(fromClass, members) {
367 var prototype = this.prototype,
368 fromPrototype = fromClass.prototype,
369 //<debug>
370 className = Ext.getClassName(this),
371 //</debug>
372 i, ln, name, fn, toBorrow;
373
374 members = Ext.Array.from(members);
375
376 for (i = 0,ln = members.length; i < ln; i++) {
377 name = members[i];
378
379 toBorrow = fromPrototype[name];
380
381 if (typeof toBorrow == 'function') {
382 fn = function() {
383 return toBorrow.apply(this, arguments);
384 };
385
386 //<debug>
387 if (className) {
388 fn.displayName = className + '#' + name;
389 }
390 //</debug>
391
392 fn.$owner = this;
393 fn.$name = name;
394
395 prototype[name] = fn;
396 }
397 else {
398 prototype[name] = toBorrow;
399 }
400 }
401
402 return this;
403 },
404
405 /**
406 * Override members of this class. Overridden methods can be invoked via
407 * {@link Ext.Base#callParent}.
408 *
409 * Ext.define('My.Cat', {
410 * constructor: function() {
411 * alert("I'm a cat!");
412 * }
413 * });
414 *
415 * My.Cat.override({
416 * constructor: function() {
417 * alert("I'm going to be a cat!");
418 *
419 * var instance = this.callParent(arguments);
420 *
421 * alert("Meeeeoooowwww");
422 *
423 * return instance;
424 * }
425 * });
426 *
427 * var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
428 * // alerts "I'm a cat!"
429 * // alerts "Meeeeoooowwww"
430 *
431 * As of 2.1, direct use of this method is deprecated. Use {@link Ext#define Ext.define}
432 * instead:
433 *
434 * Ext.define('My.CatOverride', {
435 * override: 'My.Cat',
436 *
437 * constructor: function() {
438 * alert("I'm going to be a cat!");
439 *
440 * var instance = this.callParent(arguments);
441 *
442 * alert("Meeeeoooowwww");
443 *
444 * return instance;
445 * }
446 * });
447 *
448 * The above accomplishes the same result but can be managed by the {@link Ext.Loader}
449 * which can properly order the override and its target class and the build process
450 * can determine whether the override is needed based on the required state of the
451 * target class (My.Cat).
452 *
453 * @param {Object} members The properties to add to this class. This should be
454 * specified as an object literal containing one or more properties.
455 * @return {Ext.Base} this class
456 * @static
457 * @inheritable
458 * @deprecated 2.1.0 Please use {@link Ext#define Ext.define} instead
459 */
460 override: function(members) {
461 var me = this,
462 enumerables = Ext.enumerables,
463 target = me.prototype,
464 cloneFunction = Ext.Function.clone,
465 currentConfig = target.config,
466 name, index, member, statics, names, previous, newConfig, prop;
467
468 if (arguments.length === 2) {
469 name = members;
470 members = {};
471 members[name] = arguments[1];
472 enumerables = null;
473 }
474
475 do {
476 names = []; // clean slate for prototype (1st pass) and static (2nd pass)
477 statics = null; // not needed 1st pass, but needs to be cleared for 2nd pass
478
479 for (name in members) { // hasOwnProperty is checked in the next loop...
480 if (name == 'statics') {
481 statics = members[name];
482 }
483 else if (name == 'config') {
484 newConfig = members[name];
485 //<debug error>
486 for (prop in newConfig) {
487 if (!(prop in currentConfig)) {
488 throw new Error("Attempting to override a non-existant config property. This is not " +
489 "supported, you must extend the Class.");
490 }
491 }
492 //</debug>
493 me.addConfig(newConfig, true);
494 }
495 else {
496 names.push(name);
497 }
498 }
499
500 if (enumerables) {
501 names.push.apply(names, enumerables);
502 }
503
504 for (index = names.length; index--; ) {
505 name = names[index];
506
507 if (members.hasOwnProperty(name)) {
508 member = members[name];
509
510 if (typeof member == 'function' && !member.$className && member !== Ext.emptyFn) {
511 if (typeof member.$owner != 'undefined') {
512 member = cloneFunction(member);
513 }
514
515 //<debug>
516 var className = me.$className;
517 if (className) {
518 member.displayName = className + '#' + name;
519 }
520 //</debug>
521
522 member.$owner = me;
523 member.$name = name;
524
525 previous = target[name];
526 if (previous) {
527 member.$previous = previous;
528 }
529 }
530
531 target[name] = member;
532 }
533 }
534
535 target = me; // 2nd pass is for statics
536 members = statics; // statics will be null on 2nd pass
537 } while (members);
538
539 return this;
540 },
541
542 /**
543 * @protected
544 * @static
545 * @inheritable
546 */
547 callParent: function(args) {
548 var method;
549
550 // This code is intentionally inlined for the least amount of debugger stepping
551 return (method = this.callParent.caller) && (method.$previous ||
552 ((method = method.$owner ? method : method.caller) &&
553 method.$owner.superclass.$class[method.$name])).apply(this, args || noArgs);
554 },
555
556 //<feature classSystem.mixins>
557 /**
558 * Used internally by the mixins pre-processor
559 * @private
560 * @static
561 * @inheritable
562 */
563 mixin: function(name, mixinClass) {
564 var mixin = mixinClass.prototype,
565 prototype = this.prototype,
566 key;
567
568 if (typeof mixin.onClassMixedIn != 'undefined') {
569 mixin.onClassMixedIn.call(mixinClass, this);
570 }
571
572 if (!prototype.hasOwnProperty('mixins')) {
573 if ('mixins' in prototype) {
574 prototype.mixins = Ext.Object.chain(prototype.mixins);
575 }
576 else {
577 prototype.mixins = {};
578 }
579 }
580
581 for (key in mixin) {
582 if (key === 'mixins') {
583 Ext.merge(prototype.mixins, mixin[key]);
584 }
585 else if (typeof prototype[key] == 'undefined' && key != 'mixinId' && key != 'config') {
586 prototype[key] = mixin[key];
587 }
588 }
589
590 //<feature classSystem.config>
591 if ('config' in mixin) {
592 this.addConfig(mixin.config, false);
593 }
594 //</feature>
595
596 prototype.mixins[name] = mixin;
597 },
598 //</feature>
599
600 /**
601 * Get the current class' name in string format.
602 *
603 * Ext.define('My.cool.Class', {
604 * constructor: function() {
605 * alert(this.self.getName()); // alerts 'My.cool.Class'
606 * }
607 * });
608 *
609 * My.cool.Class.getName(); // 'My.cool.Class'
610 *
611 * @return {String} className
612 * @static
613 * @inheritable
614 */
615 getName: function() {
616 return Ext.getClassName(this);
617 },
618
619 /**
620 * Create aliases for existing prototype methods. Example:
621 *
622 * Ext.define('My.cool.Class', {
623 * method1: function() { },
624 * method2: function() { }
625 * });
626 *
627 * var test = new My.cool.Class();
628 *
629 * My.cool.Class.createAlias({
630 * method3: 'method1',
631 * method4: 'method2'
632 * });
633 *
634 * test.method3(); // test.method1()
635 *
636 * My.cool.Class.createAlias('method5', 'method3');
637 *
638 * test.method5(); // test.method3() -> test.method1()
639 *
640 * @param {String/Object} alias The new method name, or an object to set multiple aliases. See
641 * {@link Ext.Function#flexSetter flexSetter}
642 * @param {String/Object} origin The original method name
643 * @static
644 * @inheritable
645 * @method
646 */
647 createAlias: flexSetter(function(alias, origin) {
648 this.override(alias, function() {
649 return this[origin].apply(this, arguments);
650 });
651 }),
652
653 /**
654 * @private
655 * @static
656 * @inheritable
657 */
658 addXtype: function(xtype) {
659 var prototype = this.prototype,
660 xtypesMap = prototype.xtypesMap,
661 xtypes = prototype.xtypes,
662 xtypesChain = prototype.xtypesChain;
663
664 if (!prototype.hasOwnProperty('xtypesMap')) {
665 xtypesMap = prototype.xtypesMap = Ext.merge({}, prototype.xtypesMap || {});
666 xtypes = prototype.xtypes = prototype.xtypes ? [].concat(prototype.xtypes) : [];
667 xtypesChain = prototype.xtypesChain = prototype.xtypesChain ? [].concat(prototype.xtypesChain) : [];
668 prototype.xtype = xtype;
669 }
670
671 if (!xtypesMap[xtype]) {
672 xtypesMap[xtype] = true;
673 xtypes.push(xtype);
674 xtypesChain.push(xtype);
675 Ext.ClassManager.setAlias(this, 'widget.' + xtype);
676 }
677
678 return this;
679 }
680 });
681
682 Base.implement({
683 isInstance: true,
684
685 $className: 'Ext.Base',
686
687 configClass: Ext.emptyFn,
688
689 initConfigList: [],
690
691 initConfigMap: {},
692
693 /**
694 * Get the reference to the class from which this object was instantiated. Note that unlike {@link Ext.Base#self},
695 * `this.statics()` is scope-independent and it always returns the class from which it was called, regardless of what
696 * `this` points to during run-time
697 *
698 * Ext.define('My.Cat', {
699 * statics: {
700 * totalCreated: 0,
701 * speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
702 * },
703 *
704 * constructor: function() {
705 * var statics = this.statics();
706 *
707 * alert(statics.speciesName); // always equals to 'Cat' no matter what 'this' refers to
708 * // equivalent to: My.Cat.speciesName
709 *
710 * alert(this.self.speciesName); // dependent on 'this'
711 *
712 * statics.totalCreated++;
713 * },
714 *
715 * clone: function() {
716 * var cloned = new this.self(); // dependent on 'this'
717 *
718 * cloned.groupName = this.statics().speciesName; // equivalent to: My.Cat.speciesName
719 *
720 * return cloned;
721 * }
722 * });
723 *
724 *
725 * Ext.define('My.SnowLeopard', {
726 * extend: 'My.Cat',
727 *
728 * statics: {
729 * speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
730 * },
731 *
732 * constructor: function() {
733 * this.callParent();
734 * }
735 * });
736 *
737 * var cat = new My.Cat(); // alerts 'Cat', then alerts 'Cat'
738 *
739 * var snowLeopard = new My.SnowLeopard(); // alerts 'Cat', then alerts 'Snow Leopard'
740 *
741 * var clone = snowLeopard.clone();
742 * alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
743 * alert(clone.groupName); // alerts 'Cat'
744 *
745 * alert(My.Cat.totalCreated); // alerts 3
746 *
747 * @protected
748 * @return {Ext.Class}
749 */
750 statics: function() {
751 var method = this.statics.caller,
752 self = this.self;
753
754 if (!method) {
755 return self;
756 }
757
758 return method.$owner;
759 },
760
761 /**
762 * Call the "parent" method of the current method. That is the method previously
763 * overridden by derivation or by an override (see {@link Ext#define}).
764 *
765 * Ext.define('My.Base', {
766 * constructor: function (x) {
767 * this.x = x;
768 * },
769 *
770 * statics: {
771 * method: function (x) {
772 * return x;
773 * }
774 * }
775 * });
776 *
777 * Ext.define('My.Derived', {
778 * extend: 'My.Base',
779 *
780 * constructor: function () {
781 * this.callParent([21]);
782 * }
783 * });
784 *
785 * var obj = new My.Derived();
786 *
787 * alert(obj.x); // alerts 21
788 *
789 * This can be used with an override as follows:
790 *
791 * Ext.define('My.DerivedOverride', {
792 * override: 'My.Derived',
793 *
794 * constructor: function (x) {
795 * this.callParent([x*2]); // calls original My.Derived constructor
796 * }
797 * });
798 *
799 * var obj = new My.Derived();
800 *
801 * alert(obj.x); // now alerts 42
802 *
803 * This also works with static methods.
804 *
805 * Ext.define('My.Derived2', {
806 * extend: 'My.Base',
807 *
808 * statics: {
809 * method: function (x) {
810 * return this.callParent([x*2]); // calls My.Base.method
811 * }
812 * }
813 * });
814 *
815 * alert(My.Base.method(10)); // alerts 10
816 * alert(My.Derived2.method(10)); // alerts 20
817 *
818 * Lastly, it also works with overridden static methods.
819 *
820 * Ext.define('My.Derived2Override', {
821 * override: 'My.Derived2',
822 *
823 * statics: {
824 * method: function (x) {
825 * return this.callParent([x*2]); // calls My.Derived2.method
826 * }
827 * }
828 * });
829 *
830 * alert(My.Derived2.method(10)); // now alerts 40
831 *
832 * To override a method and replace it and also call the superclass method, use
833 * {@link #callSuper}. This is often done to patch a method to fix a bug.
834 *
835 * @protected
836 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
837 * from the current method, for example: `this.callParent(arguments)`
838 * @return {Object} Returns the result of calling the parent method
839 */
840 callParent: function(args) {
841 // NOTE: this code is deliberately as few expressions (and no function calls)
842 // as possible so that a debugger can skip over this noise with the minimum number
843 // of steps. Basically, just hit Step Into until you are where you really wanted
844 // to be.
845 var method,
846 superMethod = (method = this.callParent.caller) && (method.$previous ||
847 ((method = method.$owner ? method : method.caller) &&
848 method.$owner.superclass[method.$name]));
849
850 //<debug error>
851 if (!superMethod) {
852 method = this.callParent.caller;
853 var parentClass, methodName;
854
855 if (!method.$owner) {
856 if (!method.caller) {
857 throw new Error("Attempting to call a protected method from the public scope, which is not allowed");
858 }
859
860 method = method.caller;
861 }
862
863 parentClass = method.$owner.superclass;
864 methodName = method.$name;
865
866 if (!(methodName in parentClass)) {
867 throw new Error("this.callParent() was called but there's no such method (" + methodName +
868 ") found in the parent class (" + (Ext.getClassName(parentClass) || 'Object') + ")");
869 }
870 }
871 //</debug>
872
873 return superMethod.apply(this, args || noArgs);
874 },
875
876 /**
877 * This method is used by an override to call the superclass method but bypass any
878 * overridden method. This is often done to "patch" a method that contains a bug
879 * but for whatever reason cannot be fixed directly.
880 *
881 * Consider:
882 *
883 * Ext.define('Ext.some.Class', {
884 * method: function () {
885 * console.log('Good');
886 * }
887 * });
888 *
889 * Ext.define('Ext.some.DerivedClass', {
890 * method: function () {
891 * console.log('Bad');
892 *
893 * // ... logic but with a bug ...
894 *
895 * this.callParent();
896 * }
897 * });
898 *
899 * To patch the bug in `DerivedClass.method`, the typical solution is to create an
900 * override:
901 *
902 * Ext.define('App.paches.DerivedClass', {
903 * override: 'Ext.some.DerivedClass',
904 *
905 * method: function () {
906 * console.log('Fixed');
907 *
908 * // ... logic but with bug fixed ...
909 *
910 * this.callSuper();
911 * }
912 * });
913 *
914 * The patch method cannot use `callParent` to call the superclass `method` since
915 * that would call the overridden method containing the bug. In other words, the
916 * above patch would only produce "Fixed" then "Good" in the console log, whereas,
917 * using `callParent` would produce "Fixed" then "Bad" then "Good".
918 *
919 * @protected
920 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
921 * from the current method, for example: `this.callSuper(arguments)`
922 * @return {Object} Returns the result of calling the superclass method
923 */
924 callSuper: function(args) {
925 var method,
926 superMethod = (method = this.callSuper.caller) && ((method = method.$owner ? method : method.caller) &&
927 method.$owner.superclass[method.$name]);
928
929 //<debug error>
930 if (!superMethod) {
931 method = this.callSuper.caller;
932 var parentClass, methodName;
933
934 if (!method.$owner) {
935 if (!method.caller) {
936 throw new Error("Attempting to call a protected method from the public scope, which is not allowed");
937 }
938
939 method = method.caller;
940 }
941
942 parentClass = method.$owner.superclass;
943 methodName = method.$name;
944
945 if (!(methodName in parentClass)) {
946 throw new Error("this.callSuper() was called but there's no such method (" + methodName +
947 ") found in the parent class (" + (Ext.getClassName(parentClass) || 'Object') + ")");
948 }
949 }
950 //</debug>
951
952 return superMethod.apply(this, args || noArgs);
953 },
954
955 /**
956 * Call the original method that was previously overridden with {@link Ext.Base#override},
957 *
958 * This method is deprecated as {@link #callParent} does the same thing.
959 *
960 * Ext.define('My.Cat', {
961 * constructor: function() {
962 * alert("I'm a cat!");
963 * }
964 * });
965 *
966 * My.Cat.override({
967 * constructor: function() {
968 * alert("I'm going to be a cat!");
969 *
970 * var instance = this.callOverridden();
971 *
972 * alert("Meeeeoooowwww");
973 *
974 * return instance;
975 * }
976 * });
977 *
978 * var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
979 * // alerts "I'm a cat!"
980 * // alerts "Meeeeoooowwww"
981 *
982 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
983 * from the current method, for example: `this.callOverridden(arguments)`
984 * @return {Object} Returns the result of calling the overridden method
985 * @protected
986 */
987 callOverridden: function(args) {
988 var method = this.callOverridden.caller;
989 return method && method.$previous.apply(this, args || noArgs);
990 },
991
992 /**
993 * @property {Ext.Class} self
994 *
995 * Get the reference to the current class from which this object was instantiated. Unlike {@link Ext.Base#statics},
996 * `this.self` is scope-dependent and it's meant to be used for dynamic inheritance. See {@link Ext.Base#statics}
997 * for a detailed comparison
998 *
999 * Ext.define('My.Cat', {
1000 * statics: {
1001 * speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
1002 * },
1003 *
1004 * constructor: function() {
1005 * alert(this.self.speciesName); // dependent on 'this'
1006 * },
1007 *
1008 * clone: function() {
1009 * return new this.self();
1010 * }
1011 * });
1012 *
1013 *
1014 * Ext.define('My.SnowLeopard', {
1015 * extend: 'My.Cat',
1016 * statics: {
1017 * speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
1018 * }
1019 * });
1020 *
1021 * var cat = new My.Cat(); // alerts 'Cat'
1022 * var snowLeopard = new My.SnowLeopard(); // alerts 'Snow Leopard'
1023 *
1024 * var clone = snowLeopard.clone();
1025 * alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
1026 *
1027 * @protected
1028 */
1029 self: Base,
1030
1031 // Default constructor, simply returns `this`
1032 constructor: function() {
1033 return this;
1034 },
1035
1036 //<feature classSystem.config>
1037
1038 wasInstantiated: false,
1039
1040 /**
1041 * Initialize configuration for this class. a typical example:
1042 *
1043 * Ext.define('My.awesome.Class', {
1044 * // The default config
1045 * config: {
1046 * name: 'Awesome',
1047 * isAwesome: true
1048 * },
1049 *
1050 * constructor: function(config) {
1051 * this.initConfig(config);
1052 * }
1053 * });
1054 *
1055 * var awesome = new My.awesome.Class({
1056 * name: 'Super Awesome'
1057 * });
1058 *
1059 * alert(awesome.getName()); // 'Super Awesome'
1060 *
1061 * @protected
1062 * @param {Object} instanceConfig
1063 * @return {Object} mixins The mixin prototypes as key - value pairs
1064 */
1065 initConfig: function(instanceConfig) {
1066 //<debug>
1067 // if (instanceConfig && instanceConfig.breakOnInitConfig) {
1068 // debugger;
1069 // }
1070 //</debug>
1071 var configNameCache = Ext.Class.configNameCache,
1072 prototype = this.self.prototype,
1073 initConfigList = this.initConfigList,
1074 initConfigMap = this.initConfigMap,
1075 config = new this.configClass,
1076 defaultConfig = this.defaultConfig,
1077 i, ln, name, value, nameMap, getName;
1078
1079 this.initConfig = Ext.emptyFn;
1080
1081 this.initialConfig = instanceConfig || {};
1082
1083 if (instanceConfig) {
1084 Ext.merge(config, instanceConfig);
1085 }
1086
1087 this.config = config;
1088
1089 // Optimize initConfigList *once* per class based on the existence of apply* and update* methods
1090 // Happens only once during the first instantiation
1091 if (!prototype.hasOwnProperty('wasInstantiated')) {
1092 prototype.wasInstantiated = true;
1093
1094 for (i = 0,ln = initConfigList.length; i < ln; i++) {
1095 name = initConfigList[i];
1096 nameMap = configNameCache[name];
1097 value = defaultConfig[name];
1098
1099 if (!(nameMap.apply in prototype)
1100 && !(nameMap.update in prototype)
1101 && prototype[nameMap.set].$isDefault
1102 && typeof value != 'object') {
1103 prototype[nameMap.internal] = defaultConfig[name];
1104 initConfigMap[name] = false;
1105 Ext.Array.remove(initConfigList, name);
1106 i--;
1107 ln--;
1108 }
1109 }
1110 }
1111
1112 if (instanceConfig) {
1113 initConfigList = initConfigList.slice();
1114
1115 for (name in instanceConfig) {
1116 if (name in defaultConfig && !initConfigMap[name]) {
1117 initConfigList.push(name);
1118 }
1119 }
1120 }
1121
1122 // Point all getters to the initGetters
1123 for (i = 0,ln = initConfigList.length; i < ln; i++) {
1124 name = initConfigList[i];
1125 nameMap = configNameCache[name];
1126 this[nameMap.get] = this[nameMap.initGet];
1127 }
1128
1129 this.beforeInitConfig(config);
1130
1131 for (i = 0,ln = initConfigList.length; i < ln; i++) {
1132 name = initConfigList[i];
1133 nameMap = configNameCache[name];
1134 getName = nameMap.get;
1135
1136 if (this.hasOwnProperty(getName)) {
1137 this[nameMap.set].call(this, config[name]);
1138 delete this[getName];
1139 }
1140 }
1141
1142 return this;
1143 },
1144
1145 beforeInitConfig: Ext.emptyFn,
1146
1147 /**
1148 * @private
1149 */
1150 getCurrentConfig: function() {
1151 var defaultConfig = this.defaultConfig,
1152 configNameCache = Ext.Class.configNameCache,
1153 config = {},
1154 name, nameMap;
1155
1156 for (name in defaultConfig) {
1157 nameMap = configNameCache[name];
1158 config[name] = this[nameMap.get].call(this);
1159 }
1160
1161 return config;
1162 },
1163
1164 /**
1165 * @private
1166 */
1167 setConfig: function(config, applyIfNotSet) {
1168 if (!config) {
1169 return this;
1170 }
1171
1172 var configNameCache = Ext.Class.configNameCache,
1173 currentConfig = this.config,
1174 defaultConfig = this.defaultConfig,
1175 initialConfig = this.initialConfig,
1176 configList = [],
1177 name, i, ln, nameMap;
1178
1179 applyIfNotSet = Boolean(applyIfNotSet);
1180
1181 for (name in config) {
1182 if ((applyIfNotSet && (name in initialConfig))) {
1183 continue;
1184 }
1185
1186 currentConfig[name] = config[name];
1187
1188 if (name in defaultConfig) {
1189 configList.push(name);
1190 nameMap = configNameCache[name];
1191 this[nameMap.get] = this[nameMap.initGet];
1192 }
1193 }
1194
1195 for (i = 0,ln = configList.length; i < ln; i++) {
1196 name = configList[i];
1197 nameMap = configNameCache[name];
1198 this[nameMap.set].call(this, config[name]);
1199 delete this[nameMap.get];
1200 }
1201
1202 return this;
1203 },
1204
1205 set: function(name, value) {
1206 return this[Ext.Class.configNameCache[name].set].call(this, value);
1207 },
1208
1209 get: function(name) {
1210 return this[Ext.Class.configNameCache[name].get].call(this);
1211 },
1212
1213 /**
1214 * @private
1215 */
1216 getConfig: function(name) {
1217 return this[Ext.Class.configNameCache[name].get].call(this);
1218 },
1219
1220 /**
1221 * @private
1222 */
1223 hasConfig: function(name) {
1224 return (name in this.defaultConfig);
1225 },
1226
1227 /**
1228 * Returns the initial configuration passed to constructor.
1229 *
1230 * @param {String} [name] When supplied, value for particular configuration
1231 * option is returned, otherwise the full config object is returned.
1232 * @return {Object/Mixed}
1233 */
1234 getInitialConfig: function(name) {
1235 var config = this.config;
1236
1237 if (!name) {
1238 return config;
1239 }
1240 else {
1241 return config[name];
1242 }
1243 },
1244
1245 /**
1246 * @private
1247 */
1248 onConfigUpdate: function(names, callback, scope) {
1249 var self = this.self,
1250 //<debug>
1251 className = self.$className,
1252 //</debug>
1253 i, ln, name,
1254 updaterName, updater, newUpdater;
1255
1256 names = Ext.Array.from(names);
1257
1258 scope = scope || this;
1259
1260 for (i = 0,ln = names.length; i < ln; i++) {
1261 name = names[i];
1262 updaterName = 'update' + Ext.String.capitalize(name);
1263 updater = this[updaterName] || Ext.emptyFn;
1264 newUpdater = function() {
1265 updater.apply(this, arguments);
1266 scope[callback].apply(scope, arguments);
1267 };
1268 newUpdater.$name = updaterName;
1269 newUpdater.$owner = self;
1270 //<debug>
1271 newUpdater.displayName = className + '#' + updaterName;
1272 //</debug>
1273
1274 this[updaterName] = newUpdater;
1275 }
1276 },
1277 //</feature>
1278
1279 /**
1280 * @private
1281 * @param {String} name
1282 * @param {Mixed} value
1283 * @return {Mixed}
1284 */
1285 link: function(name, value) {
1286 this.$links = {};
1287 this.link = this.doLink;
1288 return this.link.apply(this, arguments);
1289 },
1290
1291 doLink: function(name, value) {
1292 this.$links[name] = true;
1293
1294 this[name] = value;
1295
1296 return value;
1297 },
1298
1299 /**
1300 * @private
1301 */
1302 unlink: function() {
1303 var i, ln, link, value;
1304
1305 for (i = 0, ln = arguments.length; i < ln; i++) {
1306 link = arguments[i];
1307 if (this.hasOwnProperty(link)) {
1308 value = this[link];
1309 if (value) {
1310 if (value.isInstance && !value.isDestroyed) {
1311 value.destroy();
1312 }
1313 else if (value.parentNode && 'nodeType' in value) {
1314 value.parentNode.removeChild(value);
1315 }
1316 }
1317 delete this[link];
1318 }
1319 }
1320
1321 return this;
1322 },
1323
1324 /**
1325 * @protected
1326 */
1327 destroy: function() {
1328 this.destroy = Ext.emptyFn;
1329 this.isDestroyed = true;
1330
1331 if (this.hasOwnProperty('$links')) {
1332 this.unlink.apply(this, Ext.Object.getKeys(this.$links));
1333 delete this.$links;
1334 }
1335 }
1336 });
1337
1338 Ext.Base = Base;
1339
1340 })(Ext.Function.flexSetter);