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-Class'>/**
19 </span> * @author Jacky Nguyen <jacky@sencha.com>
20 * @docauthor Jacky Nguyen <jacky@sencha.com>
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.
29 * Ext.define(className, properties);
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.
34 * Ext.define('Person', {
37 * constructor: function(name) {
45 * eat: function(foodType) {
46 * alert("I'm eating: " + foodType);
52 * var aaron = new Person("Aaron");
53 * aaron.eat("Sandwich"); // alert("I'm eating: Sandwich");
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.
60 * Ext.define('Developer', {
63 * constructor: function(name, isGeek) {
64 * this.isGeek = isGeek;
66 * // Apply a method from the parent class' prototype
67 * this.callParent([name]);
73 * code: function(language) {
74 * alert("I'm coding in: " + language);
76 * this.eat("Bugs");
82 * var jacky = new Developer("Jacky", true);
83 * jacky.code("JavaScript"); // alert("I'm coding in: JavaScript");
84 * // alert("I'm eating: Bugs");
86 * See {@link Ext.Base#callParent} for more details on calling superclass' methods
90 * Ext.define('CanPlayGuitar', {
91 * playGuitar: function() {
92 * alert("F#...G...D...A");
96 * Ext.define('CanComposeSongs', {
97 * composeSongs: function() { ... }
100 * Ext.define('CanSing', {
102 * alert("I'm on the highway to hell...")
106 * Ext.define('Musician', {
110 * canPlayGuitar: 'CanPlayGuitar',
111 * canComposeSongs: 'CanComposeSongs',
116 * Ext.define('CoolPerson', {
120 * canPlayGuitar: 'CanPlayGuitar',
125 * alert("Ahem....");
127 * this.mixins.canSing.sing.call(this);
129 * alert("[Playing guitar at the same time...]");
135 * var me = new CoolPerson("Jacky");
137 * me.sing(); // alert("Ahem...");
138 * // alert("I'm on the highway to hell...");
139 * // alert("[Playing guitar at the same time...]");
140 * // alert("F#...G...D...A");
144 * Ext.define('SmartPhone', {
146 * hasTouchScreen: false,
147 * operatingSystem: 'Other',
151 * isExpensive: false,
153 * constructor: function(config) {
154 * this.initConfig(config);
159 * applyPrice: function(price) {
160 * this.isExpensive = (price > 500);
165 * applyOperatingSystem: function(operatingSystem) {
166 * if (!(/^(iOS|Android|BlackBerry)$/i).test(operatingSystem)) {
170 * return operatingSystem;
174 * var iPhone = new SmartPhone({
175 * hasTouchScreen: true,
176 * operatingSystem: 'iOS'
179 * iPhone.getPrice(); // 500;
180 * iPhone.getOperatingSystem(); // 'iOS'
181 * iPhone.getHasTouchScreen(); // true;
182 * iPhone.hasTouchScreen(); // true
184 * iPhone.isExpensive; // false;
185 * iPhone.setPrice(600);
186 * iPhone.getPrice(); // 600
187 * iPhone.isExpensive; // true;
189 * iPhone.setOperatingSystem('AlienOS');
190 * iPhone.getOperatingSystem(); // 'Other'
194 * Ext.define('Computer', {
196 * factory: function(brand) {
197 * // 'this' in static methods refer to the class itself
198 * return new this(brand);
202 * constructor: function() { ... }
205 * var dellComputer = Computer.factory('Dell');
207 * Also see {@link Ext.Base#statics} and {@link Ext.Base#self} for more details on accessing
208 * static properties within class methods
215 baseStaticProperties = [],
218 for (baseStaticProperty in Base) {
219 if (Base.hasOwnProperty(baseStaticProperty)) {
220 baseStaticProperties.push(baseStaticProperty);
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
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);
244 var preprocessorStack = classData.preprocessors || Class.getDefaultPreprocessors(),
245 registeredPreprocessors = Class.getPreprocessors(),
248 preprocessor, preprocessors, staticPropertyName, process, i, j, ln;
250 for (i = 0, ln = baseStaticProperties.length; i < ln; i++) {
251 staticPropertyName = baseStaticProperties[i];
252 newClass[staticPropertyName] = Base[staticPropertyName];
255 delete classData.preprocessors;
257 for (j = 0, ln = preprocessorStack.length; j < ln; j++) {
258 preprocessor = preprocessorStack[j];
260 if (typeof preprocessor === 'string') {
261 preprocessor = registeredPreprocessors[preprocessor];
263 if (!preprocessor.always) {
264 if (classData.hasOwnProperty(preprocessor.name)) {
265 preprocessors.push(preprocessor.fn);
269 preprocessors.push(preprocessor.fn);
273 preprocessors.push(preprocessor);
277 classData.onClassCreated = onClassCreated;
279 classData.onBeforeClassCreated = function(cls, data) {
280 onClassCreated = data.onClassCreated;
282 delete data.onBeforeClassCreated;
283 delete data.onClassCreated;
287 if (onClassCreated) {
288 onClassCreated.call(cls, cls);
292 process = function(cls, data) {
293 preprocessor = preprocessors[index++];
296 data.onBeforeClassCreated.apply(this, arguments);
300 if (preprocessor.call(this, cls, data, process) !== false) {
301 process.apply(this, arguments);
305 process.call(Class, newClass, classData);
312 <span id='Ext-Class-property-preprocessors'> /** @private */
313 </span> preprocessors: {},
315 <span id='Ext-Class-method-registerPreprocessor'> /**
316 </span> * Register a new pre-processor to be used during the class creation process
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:
322 function(cls, data, fn) {
325 // Execute this when the processing is finished.
326 // Asynchronous processing is perfectly ok
328 fn.call(this, cls, data);
332 * Passed arguments for this function are:
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 <b>must</b> to be executed when this pre-processor finishes,
337 * regardless of whether the processing is synchronous or aynchronous
339 * @return {Ext.Class} this
342 registerPreprocessor: function(name, fn, always) {
343 this.preprocessors[name] = {
345 always: always || false,
352 <span id='Ext-Class-method-getPreprocessor'> /**
353 </span> * Retrieve a pre-processor callback function by its name, which has been registered before
355 * @param {String} name
356 * @return {Function} preprocessor
358 getPreprocessor: function(name) {
359 return this.preprocessors[name];
362 getPreprocessors: function() {
363 return this.preprocessors;
366 <span id='Ext-Class-method-getDefaultPreprocessors'> /**
367 </span> * Retrieve the array stack of default pre-processors
369 * @return {Function} defaultPreprocessors
371 getDefaultPreprocessors: function() {
372 return this.defaultPreprocessors || [];
375 <span id='Ext-Class-method-setDefaultPreprocessors'> /**
376 </span> * Set the default array stack of default pre-processors
378 * @param {Array} preprocessors
379 * @return {Ext.Class} this
381 setDefaultPreprocessors: function(preprocessors) {
382 this.defaultPreprocessors = Ext.Array.from(preprocessors);
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:
391 Ext.Class.registerPreprocessor('debug', function(cls, data, fn) {
395 fn.call(this, cls, data);
397 }).insertDefaultPreprocessor('debug', 'last');
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
407 setDefaultPreprocessorPosition: function(name, offset, relativeName) {
408 var defaultPreprocessors = this.defaultPreprocessors,
411 if (typeof offset === 'string') {
412 if (offset === 'first') {
413 defaultPreprocessors.unshift(name);
417 else if (offset === 'last') {
418 defaultPreprocessors.push(name);
423 offset = (offset === 'after') ? 1 : -1;
426 index = Ext.Array.indexOf(defaultPreprocessors, relativeName);
429 defaultPreprocessors.splice(Math.max(0, index + offset), 0, name);
436 Class.registerPreprocessor('extend', function(cls, data) {
437 var extend = data.extend,
439 basePrototype = base.prototype,
440 prototype = function() {},
441 parent, i, k, ln, staticName, parentStatics,
442 parentPrototype, clsPrototype;
444 if (extend && extend !== Object) {
451 parentPrototype = parent.prototype;
453 prototype.prototype = parentPrototype;
454 clsPrototype = cls.prototype = new prototype();
456 if (!('$class' in parent)) {
457 for (i in basePrototype) {
458 if (!parentPrototype[i]) {
459 parentPrototype[i] = basePrototype[i];
464 clsPrototype.self = cls;
466 cls.superclass = clsPrototype.superclass = parentPrototype;
470 // Statics inheritance
471 parentStatics = parentPrototype.$inheritableStatics;
474 for (k = 0, ln = parentStatics.length; k < ln; k++) {
475 staticName = parentStatics[k];
477 if (!cls.hasOwnProperty(staticName)) {
478 cls[staticName] = parent[staticName];
483 // Merge the parent class' config object without referencing it
484 if (parentPrototype.config) {
485 clsPrototype.config = Ext.Object.merge({}, parentPrototype.config);
488 clsPrototype.config = {};
491 if (clsPrototype.$onExtended) {
492 clsPrototype.$onExtended.call(cls, cls, data);
495 if (data.onClassExtended) {
496 clsPrototype.$onExtended = data.onClassExtended;
497 delete data.onClassExtended;
502 Class.registerPreprocessor('statics', function(cls, data) {
503 var statics = data.statics,
506 for (name in statics) {
507 if (statics.hasOwnProperty(name)) {
508 cls[name] = statics[name];
515 Class.registerPreprocessor('inheritableStatics', function(cls, data) {
516 var statics = data.inheritableStatics,
518 prototype = cls.prototype,
521 inheritableStatics = prototype.$inheritableStatics;
523 if (!inheritableStatics) {
524 inheritableStatics = prototype.$inheritableStatics = [];
527 for (name in statics) {
528 if (statics.hasOwnProperty(name)) {
529 cls[name] = statics[name];
530 inheritableStatics.push(name);
534 delete data.inheritableStatics;
537 Class.registerPreprocessor('mixins', function(cls, data) {
538 cls.mixin(data.mixins);
543 Class.registerPreprocessor('config', function(cls, data) {
544 var prototype = cls.prototype;
546 Ext.Object.each(data.config, function(name) {
547 var cName = name.charAt(0).toUpperCase() + name.substr(1),
549 apply = 'apply' + cName,
550 setter = 'set' + cName,
551 getter = 'get' + cName;
553 if (!(apply in prototype) && !data.hasOwnProperty(apply)) {
554 data[apply] = function(val) {
559 if (!(setter in prototype) && !data.hasOwnProperty(setter)) {
560 data[setter] = function(val) {
561 var ret = this[apply].call(this, val, this[pName]);
563 if (ret !== undefined) {
571 if (!(getter in prototype) && !data.hasOwnProperty(getter)) {
572 data[getter] = function() {
578 Ext.Object.merge(prototype.config, data.config);
582 Class.setDefaultPreprocessors(['extend', 'statics', 'inheritableStatics', 'mixins', 'config']);
584 // Backwards compatible
585 Ext.extend = function(subclass, superclass, members) {
586 if (arguments.length === 2 && Ext.isObject(superclass)) {
587 members = superclass;
588 superclass = subclass;
595 Ext.Error.raise("Attempting to extend from a class which has not been loaded on the page.");
598 members.extend = superclass;
599 members.preprocessors = ['extend', 'mixins', 'config', 'statics'];
602 cls = new Class(subclass, members);
605 cls = new Class(members);
608 cls.prototype.override = function(o) {
610 if (o.hasOwnProperty(m)) {