1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-Base'>/**
2 </span> * @author Jacky Nguyen <jacky@sencha.com>
3 * @docauthor Jacky Nguyen <jacky@sencha.com>
6 * The root of all classes created with {@link Ext#define}
7 * All prototype and static members of this class are inherited by any other class
10 (function(flexSetter) {
12 var Base = Ext.Base = function() {};
14 $className: 'Ext.Base',
18 <span id='Ext-Base-property-self'> /**
19 </span> * Get the reference to the current class from which this object was instantiated. Unlike {@link Ext.Base#statics},
20 * `this.self` is scope-dependent and it's meant to be used for dynamic inheritance. See {@link Ext.Base#statics}
21 * for a detailed comparison
23 Ext.define('My.Cat', {
25 speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
28 constructor: function() {
29 alert(this.self.speciesName); / dependent on 'this'
35 return new this.self();
40 Ext.define('My.SnowLeopard', {
43 speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
47 var cat = new My.Cat(); // alerts 'Cat'
48 var snowLeopard = new My.SnowLeopard(); // alerts 'Snow Leopard'
50 var clone = snowLeopard.clone();
51 alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
59 <span id='Ext-Base-method-constructor'> /**
60 </span> * Default constructor, simply returns `this`
64 * @return {Object} this
66 constructor: function() {
70 <span id='Ext-Base-method-initConfig'> /**
71 </span> * Initialize configuration for this class. a typical example:
73 Ext.define('My.awesome.Class', {
80 constructor: function(config) {
81 this.initConfig(config);
87 var awesome = new My.awesome.Class({
91 alert(awesome.getName()); // 'Super Awesome'
94 * @param {Object} config
95 * @return {Object} mixins The mixin prototypes as key - value pairs
98 initConfig: function(config) {
99 if (!this.$configInited) {
100 this.config = Ext.Object.merge({}, this.config || {}, config || {});
102 this.applyConfig(this.config);
104 this.$configInited = true;
110 <span id='Ext-Base-method-setConfig'> /**
113 setConfig: function(config) {
114 this.applyConfig(config || {});
119 <span id='Ext-Base-property-applyConfig'> /**
122 applyConfig: flexSetter(function(name, value) {
123 var setter = 'set' + Ext.String.capitalize(name);
125 if (typeof this[setter] === 'function') {
126 this[setter].call(this, value);
132 <span id='Ext-Base-method-callParent'> /**
133 </span> * Call the parent's overridden method. For example:
135 Ext.define('My.own.A', {
136 constructor: function(test) {
141 Ext.define('My.own.B', {
144 constructor: function(test) {
147 this.callParent([test + 1]);
151 Ext.define('My.own.C', {
154 constructor: function() {
155 alert("Going to call parent's overriden constructor...");
157 this.callParent(arguments);
161 var a = new My.own.A(1); // alerts '1'
162 var b = new My.own.B(1); // alerts '1', then alerts '2'
163 var c = new My.own.C(2); // alerts "Going to call parent's overriden constructor..."
164 // alerts '2', then alerts '3'
167 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
168 * from the current method, for example: `this.callParent(arguments)`
169 * @return {Mixed} Returns the result from the superclass' method
172 callParent: function(args) {
173 var method = this.callParent.caller,
174 parentClass, methodName;
176 if (!method.$owner) {
177 //<debug error>
178 if (!method.caller) {
180 sourceClass: Ext.getClassName(this),
181 sourceMethod: "callParent",
182 msg: "Attempting to call a protected method from the public scope, which is not allowed"
187 method = method.caller;
190 parentClass = method.$owner.superclass;
191 methodName = method.$name;
193 //<debug error>
194 if (!(methodName in parentClass)) {
196 sourceClass: Ext.getClassName(this),
197 sourceMethod: methodName,
198 msg: "this.callParent() was called but there's no such method (" + methodName +
199 ") found in the parent class (" + (Ext.getClassName(parentClass) || 'Object') + ")"
204 return parentClass[methodName].apply(this, args || []);
208 <span id='Ext-Base-method-statics'> /**
209 </span> * Get the reference to the class from which this object was instantiated. Note that unlike {@link Ext.Base#self},
210 * `this.statics()` is scope-independent and it always returns the class from which it was called, regardless of what
211 * `this` points to during run-time
213 Ext.define('My.Cat', {
216 speciesName: 'Cat' // My.Cat.speciesName = 'Cat'
219 constructor: function() {
220 var statics = this.statics();
222 alert(statics.speciesName); // always equals to 'Cat' no matter what 'this' refers to
223 // equivalent to: My.Cat.speciesName
225 alert(this.self.speciesName); // dependent on 'this'
227 statics.totalCreated++;
233 var cloned = new this.self; // dependent on 'this'
235 cloned.groupName = this.statics().speciesName; // equivalent to: My.Cat.speciesName
242 Ext.define('My.SnowLeopard', {
246 speciesName: 'Snow Leopard' // My.SnowLeopard.speciesName = 'Snow Leopard'
249 constructor: function() {
254 var cat = new My.Cat(); // alerts 'Cat', then alerts 'Cat'
256 var snowLeopard = new My.SnowLeopard(); // alerts 'Cat', then alerts 'Snow Leopard'
258 var clone = snowLeopard.clone();
259 alert(Ext.getClassName(clone)); // alerts 'My.SnowLeopard'
260 alert(clone.groupName); // alerts 'Cat'
262 alert(My.Cat.totalCreated); // alerts 3
268 statics: function() {
269 var method = this.statics.caller,
276 return method.$owner;
279 <span id='Ext-Base-method-callOverridden'> /**
280 </span> * Call the original method that was previously overridden with {@link Ext.Base#override}
282 Ext.define('My.Cat', {
283 constructor: function() {
284 alert("I'm a cat!");
291 constructor: function() {
292 alert("I'm going to be a cat!");
294 var instance = this.callOverridden();
296 alert("Meeeeoooowwww");
302 var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
303 // alerts "I'm a cat!"
304 // alerts "Meeeeoooowwww"
306 * @param {Array/Arguments} args The arguments, either an array or the `arguments` object
307 * @return {Mixed} Returns the result after calling the overridden method
310 callOverridden: function(args) {
311 var method = this.callOverridden.caller;
313 //<debug error>
314 if (!method.$owner) {
316 sourceClass: Ext.getClassName(this),
317 sourceMethod: "callOverridden",
318 msg: "Attempting to call a protected method from the public scope, which is not allowed"
322 if (!method.$previous) {
324 sourceClass: Ext.getClassName(this),
325 sourceMethod: "callOverridden",
326 msg: "this.callOverridden was called in '" + method.$name +
327 "' but this method has never been overridden"
332 return method.$previous.apply(this, args || []);
335 destroy: function() {}
338 // These static properties will be copied to every newly created class with {@link Ext#define}
339 Ext.apply(Ext.Base, {
340 <span id='Ext-Base-property-create'> /**
341 </span> * Create a new instance of this Class.
342 Ext.define('My.cool.Class', {
346 My.cool.Class.create({
355 return Ext.create.apply(Ext, [this].concat(Array.prototype.slice.call(arguments, 0)));
358 <span id='Ext-Base-property-own'> /**
361 own: flexSetter(function(name, value) {
362 if (typeof value === 'function') {
363 this.ownMethod(name, value);
366 this.prototype[name] = value;
370 <span id='Ext-Base-method-ownMethod'> /**
373 ownMethod: function(name, fn) {
376 if (fn.$owner !== undefined && fn !== Ext.emptyFn) {
380 return originalFn.apply(this, arguments);
386 className = Ext.getClassName(this);
388 fn.displayName = className + '#' + name;
394 this.prototype[name] = fn;
397 <span id='Ext-Base-property-addStatics'> /**
398 </span> * Add / override static properties of this class.
400 Ext.define('My.cool.Class', {
404 My.cool.Class.addStatics({
405 someProperty: 'someValue', // My.cool.Class.someProperty = 'someValue'
406 method1: function() { ... }, // My.cool.Class.method1 = function() { ... };
407 method2: function() { ... } // My.cool.Class.method2 = function() { ... };
410 * @property addStatics
413 * @param {Object} members
416 addStatics: function(members) {
417 for (var name in members) {
418 if (members.hasOwnProperty(name)) {
419 this[name] = members[name];
426 <span id='Ext-Base-property-implement'> /**
427 </span> * Add methods / properties to the prototype of this class.
429 Ext.define('My.awesome.Cat', {
430 constructor: function() {
435 My.awesome.Cat.implement({
441 var kitty = new My.awesome.Cat;
444 * @property implement
447 * @param {Object} members
450 implement: function(members) {
451 var prototype = this.prototype,
452 name, i, member, previous;
454 var className = Ext.getClassName(this);
456 for (name in members) {
457 if (members.hasOwnProperty(name)) {
458 member = members[name];
460 if (typeof member === 'function') {
461 member.$owner = this;
465 member.displayName = className + '#' + name;
470 prototype[name] = member;
474 if (Ext.enumerables) {
475 var enumerables = Ext.enumerables;
477 for (i = enumerables.length; i--;) {
478 name = enumerables[i];
480 if (members.hasOwnProperty(name)) {
481 member = members[name];
482 member.$owner = this;
484 prototype[name] = member;
490 <span id='Ext-Base-property-borrow'> /**
491 </span> * Borrow another class' members to the prototype of this class.
495 printMoney: function() {
500 Ext.define('Thief', {
504 Thief.borrow(Bank, ['money', 'printMoney']);
506 var steve = new Thief();
508 alert(steve.money); // alerts '$$$'
509 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
519 borrow: function(fromClass, members) {
520 var fromPrototype = fromClass.prototype,
523 members = Ext.Array.from(members);
525 for (i = 0, ln = members.length; i < ln; i++) {
528 this.own(member, fromPrototype[member]);
534 <span id='Ext-Base-property-override'> /**
535 </span> * Override prototype members of this class. Overridden methods can be invoked via
536 * {@link Ext.Base#callOverridden}
538 Ext.define('My.Cat', {
539 constructor: function() {
540 alert("I'm a cat!");
547 constructor: function() {
548 alert("I'm going to be a cat!");
550 var instance = this.callOverridden();
552 alert("Meeeeoooowwww");
558 var kitty = new My.Cat(); // alerts "I'm going to be a cat!"
559 // alerts "I'm a cat!"
560 // alerts "Meeeeoooowwww"
565 * @param {Object} members
566 * @return {Ext.Base} this
569 override: function(members) {
570 var prototype = this.prototype,
571 name, i, member, previous;
573 for (name in members) {
574 if (members.hasOwnProperty(name)) {
575 member = members[name];
577 if (typeof member === 'function') {
578 if (typeof prototype[name] === 'function') {
579 previous = prototype[name];
580 member.$previous = previous;
583 this.ownMethod(name, member);
586 prototype[name] = member;
591 if (Ext.enumerables) {
592 var enumerables = Ext.enumerables;
594 for (i = enumerables.length; i--;) {
595 name = enumerables[i];
597 if (members.hasOwnProperty(name)) {
598 if (prototype[name] !== undefined) {
599 previous = prototype[name];
600 members[name].$previous = previous;
603 this.ownMethod(name, members[name]);
611 <span id='Ext-Base-property-mixin'> /**
612 </span> * Used internally by the mixins pre-processor
615 mixin: flexSetter(function(name, cls) {
616 var mixin = cls.prototype,
621 if (mixin.hasOwnProperty(i)) {
622 if (my[i] === undefined) {
623 if (typeof mixin[i] === 'function') {
626 if (fn.$owner === undefined) {
627 this.ownMethod(i, fn);
637 else if (i === 'config' && my.config && mixin.config) {
638 Ext.Object.merge(my.config, mixin.config);
643 if (my.mixins === undefined) {
647 my.mixins[name] = mixin;
650 <span id='Ext-Base-method-getName'> /**
651 </span> * Get the current class' name in string format.
653 Ext.define('My.cool.Class', {
654 constructor: function() {
655 alert(this.self.getName()); // alerts 'My.cool.Class'
659 My.cool.Class.getName(); // 'My.cool.Class'
661 * @return {String} className
664 getName: function() {
665 return Ext.getClassName(this);
668 <span id='Ext-Base-property-createAlias'> /**
669 </span> * Create aliases for existing prototype methods. Example:
671 Ext.define('My.cool.Class', {
672 method1: function() { ... },
673 method2: function() { ... }
676 var test = new My.cool.Class();
678 My.cool.Class.createAlias({
683 test.method3(); // test.method1()
685 My.cool.Class.createAlias('method5', 'method3');
687 test.method5(); // test.method3() -> test.method1()
689 * @property createAlias
692 * @param {String/Object} alias The new method name, or an object to set multiple aliases. See
693 * {@link Ext.Function#flexSetter flexSetter}
694 * @param {String/Object} origin The original method name
697 createAlias: flexSetter(function(alias, origin) {
698 this.prototype[alias] = this.prototype[origin];
702 })(Ext.Function.flexSetter);
703 </pre></pre></body></html>