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; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Base'>/**
19 </span> * @author Jacky Nguyen <jacky@sencha.com>
20 * @docauthor Jacky Nguyen <jacky@sencha.com>
23 * The root of all classes created with {@link Ext#define}
24 * All prototype and static members of this class are inherited by any other class
27 (function(flexSetter) {
29 var Base = Ext.Base = function() {};
31 $className: 'Ext.Base',
35 <span id='Ext-Base-property-self'> /**
36 </span> * Get the reference to the current class from which this object was instantiated. Unlike {@link Ext.Base#statics},
37 * `this.self` is scope-dependent and it's meant to be used for dynamic inheritance. See {@link Ext.Base#statics}
38 * for a detailed comparison
40 * Ext.define('My.Cat', {
42 * speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
45 * constructor: function() {
46 * alert(this.self.speciesName); / dependent on 'this'
52 * return new this.self();
57 * Ext.define('My.SnowLeopard', {
60 * speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
64 * var cat = new My.Cat(); // alerts 'Cat'
65 * var snowLeopard = new My.SnowLeopard(); // alerts 'Snow Leopard'
67 * var clone = snowLeopard.clone();
68 * alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
76 <span id='Ext-Base-method-constructor'> /**
77 </span> * Default constructor, simply returns `this`
81 * @return {Object} this
83 constructor: function() {
87 <span id='Ext-Base-method-initConfig'> /**
88 </span> * Initialize configuration for this class. a typical example:
90 * Ext.define('My.awesome.Class', {
91 * // The default config
97 * constructor: function(config) {
98 * this.initConfig(config);
104 * var awesome = new My.awesome.Class({
105 * name: 'Super Awesome'
108 * alert(awesome.getName()); // 'Super Awesome'
111 * @param {Object} config
112 * @return {Object} mixins The mixin prototypes as key - value pairs
115 initConfig: function(config) {
116 if (!this.$configInited) {
117 this.config = Ext.Object.merge({}, this.config || {}, config || {});
119 this.applyConfig(this.config);
121 this.$configInited = true;
127 <span id='Ext-Base-method-setConfig'> /**
130 setConfig: function(config) {
131 this.applyConfig(config || {});
136 <span id='Ext-Base-property-applyConfig'> /**
139 applyConfig: flexSetter(function(name, value) {
140 var setter = 'set' + Ext.String.capitalize(name);
142 if (typeof this[setter] === 'function') {
143 this[setter].call(this, value);
149 <span id='Ext-Base-method-callParent'> /**
150 </span> * Call the parent's overridden method. For example:
152 * Ext.define('My.own.A', {
153 * constructor: function(test) {
158 * Ext.define('My.own.B', {
159 * extend: 'My.own.A',
161 * constructor: function(test) {
164 * this.callParent([test + 1]);
168 * Ext.define('My.own.C', {
169 * extend: 'My.own.B',
171 * constructor: function() {
172 * alert("Going to call parent's overriden constructor...");
174 * this.callParent(arguments);
178 * var a = new My.own.A(1); // alerts '1'
179 * var b = new My.own.B(1); // alerts '1', then alerts '2'
180 * var c = new My.own.C(2); // alerts "Going to call parent's overriden constructor..."
181 * // alerts '2', then alerts '3'
184 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
185 * from the current method, for example: `this.callParent(arguments)`
186 * @return {Mixed} Returns the result from the superclass' method
189 callParent: function(args) {
190 var method = this.callParent.caller,
191 parentClass, methodName;
193 if (!method.$owner) {
194 //<debug error>
195 if (!method.caller) {
197 sourceClass: Ext.getClassName(this),
198 sourceMethod: "callParent",
199 msg: "Attempting to call a protected method from the public scope, which is not allowed"
204 method = method.caller;
207 parentClass = method.$owner.superclass;
208 methodName = method.$name;
210 //<debug error>
211 if (!(methodName in parentClass)) {
213 sourceClass: Ext.getClassName(this),
214 sourceMethod: methodName,
215 msg: "this.callParent() was called but there's no such method (" + methodName +
216 ") found in the parent class (" + (Ext.getClassName(parentClass) || 'Object') + ")"
221 return parentClass[methodName].apply(this, args || []);
225 <span id='Ext-Base-method-statics'> /**
226 </span> * Get the reference to the class from which this object was instantiated. Note that unlike {@link Ext.Base#self},
227 * `this.statics()` is scope-independent and it always returns the class from which it was called, regardless of what
228 * `this` points to during run-time
230 * Ext.define('My.Cat', {
233 * speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
236 * constructor: function() {
237 * var statics = this.statics();
239 * alert(statics.speciesName); // always equals to 'Cat' no matter what 'this' refers to
240 * // equivalent to: My.Cat.speciesName
242 * alert(this.self.speciesName); // dependent on 'this'
244 * statics.totalCreated++;
249 * clone: function() {
250 * var cloned = new this.self; // dependent on 'this'
252 * cloned.groupName = this.statics().speciesName; // equivalent to: My.Cat.speciesName
259 * Ext.define('My.SnowLeopard', {
263 * speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
266 * constructor: function() {
271 * var cat = new My.Cat(); // alerts 'Cat', then alerts 'Cat'
273 * var snowLeopard = new My.SnowLeopard(); // alerts 'Cat', then alerts 'Snow Leopard'
275 * var clone = snowLeopard.clone();
276 * alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
277 * alert(clone.groupName); // alerts 'Cat'
279 * alert(My.Cat.totalCreated); // alerts 3
285 statics: function() {
286 var method = this.statics.caller,
293 return method.$owner;
296 <span id='Ext-Base-method-callOverridden'> /**
297 </span> * Call the original method that was previously overridden with {@link Ext.Base#override}
299 * Ext.define('My.Cat', {
300 * constructor: function() {
301 * alert("I'm a cat!");
308 * constructor: function() {
309 * alert("I'm going to be a cat!");
311 * var instance = this.callOverridden();
313 * alert("Meeeeoooowwww");
319 * var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
320 * // alerts "I'm a cat!"
321 * // alerts "Meeeeoooowwww"
323 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
324 * @return {Mixed} Returns the result after calling the overridden method
327 callOverridden: function(args) {
328 var method = this.callOverridden.caller;
330 //<debug error>
331 if (!method.$owner) {
333 sourceClass: Ext.getClassName(this),
334 sourceMethod: "callOverridden",
335 msg: "Attempting to call a protected method from the public scope, which is not allowed"
339 if (!method.$previous) {
341 sourceClass: Ext.getClassName(this),
342 sourceMethod: "callOverridden",
343 msg: "this.callOverridden was called in '" + method.$name +
344 "' but this method has never been overridden"
349 return method.$previous.apply(this, args || []);
352 destroy: function() {}
355 // These static properties will be copied to every newly created class with {@link Ext#define}
356 Ext.apply(Ext.Base, {
357 <span id='Ext-Base-property-create'> /**
358 </span> * Create a new instance of this Class.
360 * Ext.define('My.cool.Class', {
364 * My.cool.Class.create({
374 return Ext.create.apply(Ext, [this].concat(Array.prototype.slice.call(arguments, 0)));
377 <span id='Ext-Base-property-own'> /**
380 own: flexSetter(function(name, value) {
381 if (typeof value === 'function') {
382 this.ownMethod(name, value);
385 this.prototype[name] = value;
389 <span id='Ext-Base-method-ownMethod'> /**
392 ownMethod: function(name, fn) {
395 if (fn.$owner !== undefined && fn !== Ext.emptyFn) {
399 return originalFn.apply(this, arguments);
405 className = Ext.getClassName(this);
407 fn.displayName = className + '#' + name;
413 this.prototype[name] = fn;
416 <span id='Ext-Base-property-addStatics'> /**
417 </span> * Add / override static properties of this class.
419 * Ext.define('My.cool.Class', {
423 * My.cool.Class.addStatics({
424 * someProperty: 'someValue', // My.cool.Class.someProperty = 'someValue'
425 * method1: function() { ... }, // My.cool.Class.method1 = function() { ... };
426 * method2: function() { ... } // My.cool.Class.method2 = function() { ... };
429 * @property addStatics
432 * @param {Object} members
435 addStatics: function(members) {
436 for (var name in members) {
437 if (members.hasOwnProperty(name)) {
438 this[name] = members[name];
445 <span id='Ext-Base-property-implement'> /**
446 </span> * Add methods / properties to the prototype of this class.
448 * Ext.define('My.awesome.Cat', {
449 * constructor: function() {
454 * My.awesome.Cat.implement({
456 * alert('Meowww...');
460 * var kitty = new My.awesome.Cat;
463 * @property implement
466 * @param {Object} members
469 implement: function(members) {
470 var prototype = this.prototype,
471 name, i, member, previous;
473 var className = Ext.getClassName(this);
475 for (name in members) {
476 if (members.hasOwnProperty(name)) {
477 member = members[name];
479 if (typeof member === 'function') {
480 member.$owner = this;
484 member.displayName = className + '#' + name;
489 prototype[name] = member;
493 if (Ext.enumerables) {
494 var enumerables = Ext.enumerables;
496 for (i = enumerables.length; i--;) {
497 name = enumerables[i];
499 if (members.hasOwnProperty(name)) {
500 member = members[name];
501 member.$owner = this;
503 prototype[name] = member;
509 <span id='Ext-Base-property-borrow'> /**
510 </span> * Borrow another class' members to the prototype of this class.
512 * Ext.define('Bank', {
514 * printMoney: function() {
519 * Ext.define('Thief', {
523 * Thief.borrow(Bank, ['money', 'printMoney']);
525 * var steve = new Thief();
527 * alert(steve.money); // alerts '$$$'
528 * steve.printMoney(); // alerts '$$$$$$$'
533 * @param {Ext.Base} fromClass The class to borrow members from
534 * @param {Array/String} members The names of the members to borrow
535 * @return {Ext.Base} this
538 borrow: function(fromClass, members) {
539 var fromPrototype = fromClass.prototype,
542 members = Ext.Array.from(members);
544 for (i = 0, ln = members.length; i < ln; i++) {
547 this.own(member, fromPrototype[member]);
553 <span id='Ext-Base-property-override'> /**
554 </span> * Override prototype members of this class. Overridden methods can be invoked via
555 * {@link Ext.Base#callOverridden}
557 * Ext.define('My.Cat', {
558 * constructor: function() {
559 * alert("I'm a cat!");
566 * constructor: function() {
567 * alert("I'm going to be a cat!");
569 * var instance = this.callOverridden();
571 * alert("Meeeeoooowwww");
577 * var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
578 * // alerts "I'm a cat!"
579 * // alerts "Meeeeoooowwww"
584 * @param {Object} members
585 * @return {Ext.Base} this
588 override: function(members) {
589 var prototype = this.prototype,
590 name, i, member, previous;
592 for (name in members) {
593 if (members.hasOwnProperty(name)) {
594 member = members[name];
596 if (typeof member === 'function') {
597 if (typeof prototype[name] === 'function') {
598 previous = prototype[name];
599 member.$previous = previous;
602 this.ownMethod(name, member);
605 prototype[name] = member;
610 if (Ext.enumerables) {
611 var enumerables = Ext.enumerables;
613 for (i = enumerables.length; i--;) {
614 name = enumerables[i];
616 if (members.hasOwnProperty(name)) {
617 if (prototype[name] !== undefined) {
618 previous = prototype[name];
619 members[name].$previous = previous;
622 this.ownMethod(name, members[name]);
630 <span id='Ext-Base-property-mixin'> /**
631 </span> * Used internally by the mixins pre-processor
634 mixin: flexSetter(function(name, cls) {
635 var mixin = cls.prototype,
640 if (mixin.hasOwnProperty(i)) {
641 if (my[i] === undefined) {
642 if (typeof mixin[i] === 'function') {
645 if (fn.$owner === undefined) {
646 this.ownMethod(i, fn);
656 else if (i === 'config' && my.config && mixin.config) {
657 Ext.Object.merge(my.config, mixin.config);
662 if (my.mixins === undefined) {
666 my.mixins[name] = mixin;
669 <span id='Ext-Base-method-getName'> /**
670 </span> * Get the current class' name in string format.
672 * Ext.define('My.cool.Class', {
673 * constructor: function() {
674 * alert(this.self.getName()); // alerts 'My.cool.Class'
678 * My.cool.Class.getName(); // 'My.cool.Class'
680 * @return {String} className
683 getName: function() {
684 return Ext.getClassName(this);
687 <span id='Ext-Base-property-createAlias'> /**
688 </span> * Create aliases for existing prototype methods. Example:
690 * Ext.define('My.cool.Class', {
691 * method1: function() { ... },
692 * method2: function() { ... }
695 * var test = new My.cool.Class();
697 * My.cool.Class.createAlias({
698 * method3: 'method1',
702 * test.method3(); // test.method1()
704 * My.cool.Class.createAlias('method5', 'method3');
706 * test.method5(); // test.method3() -> test.method1()
708 * @property createAlias
711 * @param {String/Object} alias The new method name, or an object to set multiple aliases. See
712 * {@link Ext.Function#flexSetter flexSetter}
713 * @param {String/Object} origin The original method name
716 createAlias: flexSetter(function(alias, origin) {
717 this.prototype[alias] = this.prototype[origin];
721 })(Ext.Function.flexSetter);