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'
75 // Default constructor, simply returns `this`
76 constructor: function() {
80 <span id='Ext-Base-method-initConfig'> /**
81 </span> * Initialize configuration for this class. a typical example:
83 * Ext.define('My.awesome.Class', {
84 * // The default config
90 * constructor: function(config) {
91 * this.initConfig(config);
97 * var awesome = new My.awesome.Class({
98 * name: 'Super Awesome'
101 * alert(awesome.getName()); // 'Super Awesome'
104 * @param {Object} config
105 * @return {Object} mixins The mixin prototypes as key - value pairs
107 initConfig: function(config) {
108 if (!this.$configInited) {
109 this.config = Ext.Object.merge({}, this.config || {}, config || {});
111 this.applyConfig(this.config);
113 this.$configInited = true;
119 <span id='Ext-Base-method-setConfig'> /**
122 setConfig: function(config) {
123 this.applyConfig(config || {});
128 <span id='Ext-Base-property-applyConfig'> /**
131 applyConfig: flexSetter(function(name, value) {
132 var setter = 'set' + Ext.String.capitalize(name);
134 if (typeof this[setter] === 'function') {
135 this[setter].call(this, value);
141 <span id='Ext-Base-method-callParent'> /**
142 </span> * Call the parent's overridden method. For example:
144 * Ext.define('My.own.A', {
145 * constructor: function(test) {
150 * Ext.define('My.own.B', {
151 * extend: 'My.own.A',
153 * constructor: function(test) {
156 * this.callParent([test + 1]);
160 * Ext.define('My.own.C', {
161 * extend: 'My.own.B',
163 * constructor: function() {
164 * alert("Going to call parent's overriden constructor...");
166 * this.callParent(arguments);
170 * var a = new My.own.A(1); // alerts '1'
171 * var b = new My.own.B(1); // alerts '1', then alerts '2'
172 * var c = new My.own.C(2); // alerts "Going to call parent's overriden constructor..."
173 * // alerts '2', then alerts '3'
176 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
177 * from the current method, for example: `this.callParent(arguments)`
178 * @return {Mixed} Returns the result from the superclass' method
180 callParent: function(args) {
181 var method = this.callParent.caller,
182 parentClass, methodName;
184 if (!method.$owner) {
185 //<debug error>
186 if (!method.caller) {
188 sourceClass: Ext.getClassName(this),
189 sourceMethod: "callParent",
190 msg: "Attempting to call a protected method from the public scope, which is not allowed"
195 method = method.caller;
198 parentClass = method.$owner.superclass;
199 methodName = method.$name;
201 //<debug error>
202 if (!(methodName in parentClass)) {
204 sourceClass: Ext.getClassName(this),
205 sourceMethod: methodName,
206 msg: "this.callParent() was called but there's no such method (" + methodName +
207 ") found in the parent class (" + (Ext.getClassName(parentClass) || 'Object') + ")"
212 return parentClass[methodName].apply(this, args || []);
216 <span id='Ext-Base-method-statics'> /**
217 </span> * Get the reference to the class from which this object was instantiated. Note that unlike {@link Ext.Base#self},
218 * `this.statics()` is scope-independent and it always returns the class from which it was called, regardless of what
219 * `this` points to during run-time
221 * Ext.define('My.Cat', {
224 * speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
227 * constructor: function() {
228 * var statics = this.statics();
230 * alert(statics.speciesName); // always equals to 'Cat' no matter what 'this' refers to
231 * // equivalent to: My.Cat.speciesName
233 * alert(this.self.speciesName); // dependent on 'this'
235 * statics.totalCreated++;
240 * clone: function() {
241 * var cloned = new this.self; // dependent on 'this'
243 * cloned.groupName = this.statics().speciesName; // equivalent to: My.Cat.speciesName
250 * Ext.define('My.SnowLeopard', {
254 * speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
257 * constructor: function() {
262 * var cat = new My.Cat(); // alerts 'Cat', then alerts 'Cat'
264 * var snowLeopard = new My.SnowLeopard(); // alerts 'Cat', then alerts 'Snow Leopard'
266 * var clone = snowLeopard.clone();
267 * alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
268 * alert(clone.groupName); // alerts 'Cat'
270 * alert(My.Cat.totalCreated); // alerts 3
275 statics: function() {
276 var method = this.statics.caller,
283 return method.$owner;
286 <span id='Ext-Base-method-callOverridden'> /**
287 </span> * Call the original method that was previously overridden with {@link Ext.Base#override}
289 * Ext.define('My.Cat', {
290 * constructor: function() {
291 * alert("I'm a cat!");
298 * constructor: function() {
299 * alert("I'm going to be a cat!");
301 * var instance = this.callOverridden();
303 * alert("Meeeeoooowwww");
309 * var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
310 * // alerts "I'm a cat!"
311 * // alerts "Meeeeoooowwww"
313 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
314 * @return {Mixed} Returns the result after calling the overridden method
316 callOverridden: function(args) {
317 var method = this.callOverridden.caller;
319 //<debug error>
320 if (!method.$owner) {
322 sourceClass: Ext.getClassName(this),
323 sourceMethod: "callOverridden",
324 msg: "Attempting to call a protected method from the public scope, which is not allowed"
328 if (!method.$previous) {
330 sourceClass: Ext.getClassName(this),
331 sourceMethod: "callOverridden",
332 msg: "this.callOverridden was called in '" + method.$name +
333 "' but this method has never been overridden"
338 return method.$previous.apply(this, args || []);
341 destroy: function() {}
344 // These static properties will be copied to every newly created class with {@link Ext#define}
345 Ext.apply(Ext.Base, {
346 <span id='Ext-Base-method-create'> /**
347 </span> * Create a new instance of this Class.
349 * Ext.define('My.cool.Class', {
353 * My.cool.Class.create({
357 * All parameters are passed to the constructor of the class.
359 * @return {Object} the created instance.
363 return Ext.create.apply(Ext, [this].concat(Array.prototype.slice.call(arguments, 0)));
366 <span id='Ext-Base-property-own'> /**
369 own: flexSetter(function(name, value) {
370 if (typeof value === 'function') {
371 this.ownMethod(name, value);
374 this.prototype[name] = value;
378 <span id='Ext-Base-method-ownMethod'> /**
381 ownMethod: function(name, fn) {
384 if (fn.$owner !== undefined && fn !== Ext.emptyFn) {
388 return originalFn.apply(this, arguments);
394 className = Ext.getClassName(this);
396 fn.displayName = className + '#' + name;
402 this.prototype[name] = fn;
405 <span id='Ext-Base-method-addStatics'> /**
406 </span> * Add / override static properties of this class.
408 * Ext.define('My.cool.Class', {
412 * My.cool.Class.addStatics({
413 * someProperty: 'someValue', // My.cool.Class.someProperty = 'someValue'
414 * method1: function() { ... }, // My.cool.Class.method1 = function() { ... };
415 * method2: function() { ... } // My.cool.Class.method2 = function() { ... };
418 * @param {Object} members
419 * @return {Ext.Base} this
422 addStatics: function(members) {
423 for (var name in members) {
424 if (members.hasOwnProperty(name)) {
425 this[name] = members[name];
432 <span id='Ext-Base-method-implement'> /**
433 </span> * Add methods / properties to the prototype of this class.
435 * Ext.define('My.awesome.Cat', {
436 * constructor: function() {
441 * My.awesome.Cat.implement({
443 * alert('Meowww...');
447 * var kitty = new My.awesome.Cat;
450 * @param {Object} members
453 implement: function(members) {
454 var prototype = this.prototype,
455 name, i, member, previous;
457 var className = Ext.getClassName(this);
459 for (name in members) {
460 if (members.hasOwnProperty(name)) {
461 member = members[name];
463 if (typeof member === 'function') {
464 member.$owner = this;
468 member.displayName = className + '#' + name;
473 prototype[name] = member;
477 if (Ext.enumerables) {
478 var enumerables = Ext.enumerables;
480 for (i = enumerables.length; i--;) {
481 name = enumerables[i];
483 if (members.hasOwnProperty(name)) {
484 member = members[name];
485 member.$owner = this;
487 prototype[name] = member;
493 <span id='Ext-Base-method-borrow'> /**
494 </span> * Borrow another class' members to the prototype of this class.
496 * Ext.define('Bank', {
498 * printMoney: function() {
503 * Ext.define('Thief', {
507 * Thief.borrow(Bank, ['money', 'printMoney']);
509 * var steve = new Thief();
511 * alert(steve.money); // alerts '$$$'
512 * steve.printMoney(); // alerts '$$$$$$$'
514 * @param {Ext.Base} fromClass The class to borrow members from
515 * @param {Array/String} members The names of the members to borrow
516 * @return {Ext.Base} this
520 borrow: function(fromClass, members) {
521 var fromPrototype = fromClass.prototype,
524 members = Ext.Array.from(members);
526 for (i = 0, ln = members.length; i < ln; i++) {
529 this.own(member, fromPrototype[member]);
535 <span id='Ext-Base-method-override'> /**
536 </span> * Override prototype members of this class. Overridden methods can be invoked via
537 * {@link Ext.Base#callOverridden}
539 * Ext.define('My.Cat', {
540 * constructor: function() {
541 * alert("I'm a cat!");
548 * constructor: function() {
549 * alert("I'm going to be a cat!");
551 * var instance = this.callOverridden();
553 * alert("Meeeeoooowwww");
559 * var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
560 * // alerts "I'm a cat!"
561 * // alerts "Meeeeoooowwww"
563 * @param {Object} members
564 * @return {Ext.Base} this
567 override: function(members) {
568 var prototype = this.prototype,
569 name, i, member, previous;
571 for (name in members) {
572 if (members.hasOwnProperty(name)) {
573 member = members[name];
575 if (typeof member === 'function') {
576 if (typeof prototype[name] === 'function') {
577 previous = prototype[name];
578 member.$previous = previous;
581 this.ownMethod(name, member);
584 prototype[name] = member;
589 if (Ext.enumerables) {
590 var enumerables = Ext.enumerables;
592 for (i = enumerables.length; i--;) {
593 name = enumerables[i];
595 if (members.hasOwnProperty(name)) {
596 if (prototype[name] !== undefined) {
597 previous = prototype[name];
598 members[name].$previous = previous;
601 this.ownMethod(name, members[name]);
609 <span id='Ext-Base-property-mixin'> /**
610 </span> * Used internally by the mixins pre-processor
613 mixin: flexSetter(function(name, cls) {
614 var mixin = cls.prototype,
619 if (mixin.hasOwnProperty(i)) {
620 if (my[i] === undefined) {
621 if (typeof mixin[i] === 'function') {
624 if (fn.$owner === undefined) {
625 this.ownMethod(i, fn);
635 else if (i === 'config' && my.config && mixin.config) {
636 Ext.Object.merge(my.config, mixin.config);
641 if (my.mixins === undefined) {
645 my.mixins[name] = mixin;
648 <span id='Ext-Base-method-getName'> /**
649 </span> * Get the current class' name in string format.
651 * Ext.define('My.cool.Class', {
652 * constructor: function() {
653 * alert(this.self.getName()); // alerts 'My.cool.Class'
657 * My.cool.Class.getName(); // 'My.cool.Class'
659 * @return {String} className
661 getName: function() {
662 return Ext.getClassName(this);
665 <span id='Ext-Base-method-createAlias'> /**
666 </span> * Create aliases for existing prototype methods. Example:
668 * Ext.define('My.cool.Class', {
669 * method1: function() { ... },
670 * method2: function() { ... }
673 * var test = new My.cool.Class();
675 * My.cool.Class.createAlias({
676 * method3: 'method1',
680 * test.method3(); // test.method1()
682 * My.cool.Class.createAlias('method5', 'method3');
684 * test.method5(); // test.method3() -> test.method1()
686 * @param {String/Object} alias The new method name, or an object to set multiple aliases. See
687 * {@link Ext.Function#flexSetter flexSetter}
688 * @param {String/Object} origin The original method name
692 createAlias: flexSetter(function(alias, origin) {
693 this.prototype[alias] = this.prototype[origin];
697 })(Ext.Function.flexSetter);