X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/3789b528d8dd8aad4558e38e22d775bcab1cbd36..6746dc89c47ed01b165cc1152533605f97eb8e8d:/docs/guides/class_system/README.js diff --git a/docs/guides/class_system/README.js b/docs/guides/class_system/README.js index 38dc9cbc..3d6c6064 100644 --- a/docs/guides/class_system/README.js +++ b/docs/guides/class_system/README.js @@ -1,3 +1,3 @@ Ext.data.JsonP.class_system({ - "guide": "

Ext JS 4 Class System

\n\n
\n\n

For the first time in its history, Ext JS went through a huge refactoring from the ground up with the new class system. The new architecture stands behind almost every single class written in Ext JS 4.x, hence it's important to understand it well before you start coding.

\n\n

This manual is intended for any developer who wants to create new or extend existing classes in Ext JS 4.x. It's divided into 3 main sections:

\n\n\n\n\n

All code examples used in this manual can be downloaded here

\n\n

I. Overview

\n\n
\n\n

Ext JS 4 ships with more than 300 classes. We have a huge community of more than 200,000 developers to date, coming from various programming backgrounds all over the world. At that scale of a framework, we face a big challange of providing a common code architecture that is:

\n\n\n\n\n

JavaScript is a classless, prototype-oriented language. Hence by nature, one of the language's most powerful features is flexibility. It can get the same job done by many different ways, in many different coding styles and techniques. That feature, however, comes with the cost of unpredictability. Without a unified structure, JavaScript code can be really hard to understand, maintain and re-use.

\n\n

Class-based programming, on the other hand, still stays as the most popular model of OOP. Class-based languages usually require strong-typing, provide encapsulation, and come with standard coding convention. By generally making developers adhere to a large set of principle, written code is more likely to be predictable, extensible and scalable over time. However, they don't have the same dynamic capability found in such language as JavaScript.

\n\n

Each approach has its own pros and cons, but can we have the good parts of both at the same time while concealing the bad parts? The answer is yes, and we've implemented the solution in Ext JS 4.

\n\n

II. Naming Convention

\n\n

1) Classes

\n\n\n\n\n

2) Source Files

\n\n\n\n\n

3) Methods and Variables

\n\n\n\n\n

4) Properties

\n\n\n\n\n

II. Hands-on

\n\n
\n\n

1. Declaration

\n\n

1.1) The Old Way

\n\n

If you have ever used any previous version of Ext JS, you are certainly familiar with Ext.extend to create a class:

\n\n
var MyWindow = Ext.extend(Object, { ... });\n
\n\n

This approach is easy to followed to create a new class that inherit from another. Other than direct inheritance, however, we didn't have a fluent API for other aspects of class creation, such as configuration, statics and mixins. We will be reviewing these items in details shortly.

\n\n

Let's take a look at another example:

\n\n
My.cool.Window = Ext.extend(Ext.Window, { ... });\n
\n\n

In this example we want to namespace our new class, and make it extend from Ext.Window. There are two concerns we need to address:

\n\n
    \n
  1. My.cool needs to be an existing object before we can assign Window as its property
  2. \n
  3. Ext.Window needs to exist / loaded on the page before it can be referenced
  4. \n
\n\n\n

The first item is usually solved with Ext.namespace (aliased by Ext.ns). This method recursively transverse through the object / property tree and create them if they don't exist yet. The boring part is you need to remember adding them above Ext.extend all the time.

\n\n
Ext.ns('My.cool');\nMy.cool.Window = Ext.extend(Ext.Window, { ... });\n
\n\n

The second issue, however, is not easy to address because Ext.Window might depend on many other classes that it directly / indirectly inherits from, and in turn, these dependencies might depend on other classes to exist. For that reason, applications written before Ext JS 4 usually include the whole library in the form of ext-all.js even though they might only need a small portion of the framework.

\n\n

1.2) The New Way

\n\n

Ext JS 4 eliminates all those drawbacks with just one single method you need to remember for class creation: Ext.define. Basic syntax:

\n\n
Ext.define({String} className, {Object} members, {Function} onClassCreated);\n
\n\n\n\n\n

Example:

\n\n
Ext.define('My.sample.Person', {\n    name: 'Unknown',\n\n    constructor: function(name) {\n        if (name) {\n            this.name = name;\n        }\n\n        return this;\n    },\n\n    eat: function(foodType) {\n        alert(this.name + \" is eating: \" + foodType);\n\n        return this;\n    }\n});\n\nvar aaron = new My.sample.Person(\"Aaron\");\n    aaron.eat(\"Salad\"); // alert(\"Aaron is eating: Salad\");\n
\n\n

2. Configuration

\n\n

2.1) The Old Way

\n\n

Previous to version 4, we didn't really have a way to distinguish between class properties and user-supplied configurations. Configurations were defined as normal class properties and documented using @cfg annotation. Let's take a look at a sample class. It's rather lengthy but it well describes the problems as a whole:

\n\n
Ext.ns('My.sample');\nMy.own.Window = Ext.extend(Object, {\n   /** @readonly */\n    isWindow: true,\n\n   /** @cfg {String} title The default window's title */\n    title: 'Title Here',\n\n   /** @cfg {Object} bottomBar The default config for the bottom bar */\n    bottomBar: {\n        enabled: true,\n        height: 50,\n        resizable: false\n    },\n\n    constructor: function(config) {\n        Ext.apply(this, config || {});\n\n        this.setTitle(this.title);\n        this.setBottomBar(this.bottomBar);\n\n        return this;\n    },\n\n    setTitle: function(title) {\n        // Change title only if it's a non-empty string\n        if (!Ext.isString(title) || title.length === 0) {\n            alert('Error: Title must be a valid non-empty string');\n        }\n        else {\n            this.title = title;\n        }\n\n        return this;\n    },\n\n    getTitle: function() {\n        return this.title;\n    },\n\n    setBottomBar: function(bottomBar) {\n        // Create a new instance of My.own.WindowBottomBar if it doesn't exist\n        // Change the config of the existing instance otherwise\n        if (bottomBar && bottomBar.enabled) {\n            if (!this.bottomBar) {\n                this.bottomBar = new My.own.WindowBottomBar(bottomBar);\n            }\n            else {\n                this.bottomBar.setConfig(bottomBar);\n            }\n        }\n\n        return this;\n    },\n\n    getBottomBar: function() {\n        return this.bottomBar;\n    }\n});\n
\n\n

In short, My.own.Window:

\n\n\n\n\n

This approach has one advantage, yet it's the one drawback at the same time: you can overwrite any members of this class' instances during instantiation, including private methods and properties that should never be overwritten. The trade-off of encapsutation for flexibilty caused misusage in many applications in the past, which led to poor design as well as bad debugging and maintenance experience.

\n\n

Further more, there are other limitations:

\n\n\n\n\n

2.2) The New Way

\n\n

In Ext JS 4, we introduce a dedicated config property that gets processed by the powerful Ext.Class pre-processors before the class is created. Let's rewrite the above example:

\n\n
Ext.define('My.own.Window', {\n   /** @readonly */\n    isWindow: true,\n\n    config: {\n        title: 'Title Here',\n\n        bottomBar: {\n            enabled: true,\n            height: 50,\n            resizable: false\n        }\n    },\n\n    constructor: function(config) {\n        this.initConfig(config);\n\n        return this;\n    },\n\n    applyTitle: function(title) {\n        if (!Ext.isString(title) || title.length === 0) {\n            alert('Error: Title must be a valid non-empty string');\n        }\n        else {\n            return title;\n        }\n    },\n\n    applyBottomBar: function(bottomBar) {\n        if (bottomBar && bottomBar.enabled) {\n            if (!this.bottomBar) {\n                return new My.own.WindowBottomBar(bottomBar);\n            }\n            else {\n                this.bottomBar.setConfig(bottomBar);\n            }\n        }\n    }\n});\n
\n\n

And here's an example of how it can be used:

\n\n
var myWindow = new My.own.Window({\n    title: 'Hello World',\n    bottomBar: {\n        height: 60\n    }\n});\n\nalert(myWindow.getTitle()); // alerts \"Hello World\"\n\nmyWindow.setTitle('Something New');\n\nalert(myWindow.getTitle()); // alerts \"Something New\"\n\nmyWindow.setTitle(null); // alerts \"Error: Title must be a valid non-empty string\"\n\nmyWindow.setBottomBar({ height: 100 }); // Bottom bar's height is changed to 100\n
\n\n

With these changes:

\n\n\n\n\n

2.3) apply*

\n\n

3. Statics

\n\n

4. Inheritance

\n\n

5. Mixins

\n\n

6. Dependencies

\n\n

III. Errors Handling & Debugging

\n\n
\n\n

IV. Under the Hood

\n\n
\n\n

1. Ext.Base

\n\n

2. Ext.Class and Pre-processors

\n\n

3. Ext.ClassManager and Post-processors

\n\n

4. Ext.Loader

\n\n

See Also

\n\n\n\n\n

This guide is a work in progress.

\n\n

Please check back soon

\n" + "guide": "

Ext JS 4 Class System

\n\n
\n\n

For the first time in its history, Ext JS went through a huge refactoring from the ground up with the new class system. The new architecture stands behind almost every single class written in Ext JS 4.x, hence it's important to understand it well before you start coding.

\n\n

This manual is intended for any developer who wants to create new or extend existing classes in Ext JS 4.x. It's divided into 3 main sections:

\n\n\n\n\n

I. Overview

\n\n
\n\n

Ext JS 4 ships with more than 300 classes. We have a huge community of more than 200,000 developers to date, coming from various programming backgrounds all over the world. At that scale of a framework, we face a big challange of providing a common code architecture that is:

\n\n\n\n\n

JavaScript is a classless, prototype-oriented language. Hence by nature, one of the language's most powerful features is flexibility. It can get the same job done by many different ways, in many different coding styles and techniques. That feature, however, comes with the cost of unpredictability. Without a unified structure, JavaScript code can be really hard to understand, maintain and re-use.

\n\n

Class-based programming, on the other hand, still stays as the most popular model of OOP. Class-based languages usually require strong-typing, provide encapsulation, and come with standard coding convention. By generally making developers adhere to a large set of principle, written code is more likely to be predictable, extensible and scalable over time. However, they don't have the same dynamic capability found in such language as JavaScript.

\n\n

Each approach has its own pros and cons, but can we have the good parts of both at the same time while concealing the bad parts? The answer is yes, and we've implemented the solution in Ext JS 4.

\n\n

II. Naming Conventions

\n\n

1) Classes

\n\n\n\n\n

2) Source Files

\n\n\n\n\n

3) Methods and Variables

\n\n\n\n\n

4) Properties

\n\n\n\n\n

III. Hands-on

\n\n
\n\n

1. Declaration

\n\n

1.1) The Old Way

\n\n

If you have ever used any previous version of Ext JS, you are certainly familiar with Ext.extend to create a class:

\n\n
var MyWindow = Ext.extend(Object, { ... });\n
\n\n

This approach is easy to followed to create a new class that inherit from another. Other than direct inheritance, however, we didn't have a fluent API for other aspects of class creation, such as configuration, statics and mixins. We will be reviewing these items in details shortly.

\n\n

Let's take a look at another example:

\n\n
My.cool.Window = Ext.extend(Ext.Window, { ... });\n
\n\n

In this example we want to namespace our new class, and make it extend from Ext.Window. There are two concerns we need to address:

\n\n
    \n
  1. My.cool needs to be an existing object before we can assign Window as its property
  2. \n
  3. Ext.Window needs to exist / loaded on the page before it can be referenced
  4. \n
\n\n\n

The first item is usually solved with Ext.namespace (aliased by Ext.ns). This method recursively transverse through the object / property tree and create them if they don't exist yet. The boring part is you need to remember adding them above Ext.extend all the time.

\n\n
Ext.ns('My.cool');\nMy.cool.Window = Ext.extend(Ext.Window, { ... });\n
\n\n

The second issue, however, is not easy to address because Ext.Window might depend on many other classes that it directly / indirectly inherits from, and in turn, these dependencies might depend on other classes to exist. For that reason, applications written before Ext JS 4 usually include the whole library in the form of ext-all.js even though they might only need a small portion of the framework.

\n\n

1.2) The New Way

\n\n

Ext JS 4 eliminates all those drawbacks with just one single method you need to remember for class creation: Ext.define. Basic syntax:

\n\n
Ext.define({String} className, {Object} members, {Function} onClassCreated);\n
\n\n\n\n\n

Example:

\n\n
Ext.define('My.sample.Person', {\n    name: 'Unknown',\n\n    constructor: function(name) {\n        if (name) {\n            this.name = name;\n        }\n\n        return this;\n    },\n\n    eat: function(foodType) {\n        alert(this.name + \" is eating: \" + foodType);\n\n        return this;\n    }\n});\n\nvar aaron = Ext.create('My.sample.Person');\n    aaron.eat(\"Salad\"); // alert(\"Aaron is eating: Salad\");\n
\n\n

Note we created a new instance of My.sample.Person using the Ext.create() method. We could have used the new keyword (new My.sample.Person()). However it is recommended to get in the habit of always using Ext.create since it allows you to take advantage of dynamic loading. For more info on dynamic loading see the Getting Started guide

\n\n

2. Configuration

\n\n

2.1) The Old Way

\n\n

Previous to version 4, we didn't really have a way to distinguish between class properties and user-supplied configurations. Configurations were defined as normal class properties and documented using @cfg annotation. Let's take a look at a sample class. It's rather lengthy but it well describes the problems as a whole:

\n\n
Ext.ns('My.own');\nMy.own.Window = Ext.extend(Object, {\n   /** @readonly */\n    isWindow: true,\n\n   /** @cfg {String} title The default window's title */\n    title: 'Title Here',\n\n   /** @cfg {Object} bottomBar The default config for the bottom bar */\n    bottomBar: {\n        enabled: true,\n        height: 50,\n        resizable: false\n    },\n\n    constructor: function(config) {\n        Ext.apply(this, config || {});\n\n        this.setTitle(this.title);\n        this.setBottomBar(this.bottomBar);\n\n        return this;\n    },\n\n    setTitle: function(title) {\n        // Change title only if it's a non-empty string\n        if (!Ext.isString(title) || title.length === 0) {\n            alert('Error: Title must be a valid non-empty string');\n        }\n        else {\n            this.title = title;\n        }\n\n        return this;\n    },\n\n    getTitle: function() {\n        return this.title;\n    },\n\n    setBottomBar: function(bottomBar) {\n        // Create a new instance of My.own.WindowBottomBar if it doesn't exist\n        // Change the config of the existing instance otherwise\n        if (bottomBar && bottomBar.enabled) {\n            if (!this.bottomBar) {\n                this.bottomBar = Ext.create('My.own.WindowBottomBar', bottomBar);\n            }\n            else {\n                this.bottomBar.setConfig(bottomBar);\n            }\n        }\n\n        return this;\n    },\n\n    getBottomBar: function() {\n        return this.bottomBar;\n    }\n});\n
\n\n

In short, My.own.Window:

\n\n\n\n\n

This approach has one advantage, yet it's the one drawback at the same time: you can overwrite any members of this class' instances during instantiation, including private methods and properties that should never be overwritten. The trade-off of encapsutation for flexibilty caused misusage in many applications in the past, which led to poor design as well as bad debugging and maintenance experience.

\n\n

Further more, there are other limitations:

\n\n\n\n\n

2.2) The New Way

\n\n

In Ext JS 4, we introduce a dedicated config property that gets processed by the powerful Ext.Class pre-processors before the class is created. Let's rewrite the above example:

\n\n
Ext.define('My.own.Window', {\n   /** @readonly */\n    isWindow: true,\n\n    config: {\n        title: 'Title Here',\n\n        bottomBar: {\n            enabled: true,\n            height: 50,\n            resizable: false\n        }\n    },\n\n    constructor: function(config) {\n        this.initConfig(config);\n\n        return this;\n    },\n\n    applyTitle: function(title) {\n        if (!Ext.isString(title) || title.length === 0) {\n            alert('Error: Title must be a valid non-empty string');\n        }\n        else {\n            return title;\n        }\n    },\n\n    applyBottomBar: function(bottomBar) {\n        if (bottomBar && bottomBar.enabled) {\n            if (!this.bottomBar) {\n                return Ext.create('My.own.WindowBottomBar', bottomBar);\n            }\n            else {\n                this.bottomBar.setConfig(bottomBar);\n            }\n        }\n    }\n});\n
\n\n

And here's an example of how it can be used:

\n\n
var myWindow = Ext.create('My.own.Window', {\n    title: 'Hello World',\n    bottomBar: {\n        height: 60\n    }\n});\n\nalert(myWindow.getTitle()); // alerts \"Hello World\"\n\nmyWindow.setTitle('Something New');\n\nalert(myWindow.getTitle()); // alerts \"Something New\"\n\nmyWindow.setTitle(null); // alerts \"Error: Title must be a valid non-empty string\"\n\nmyWindow.setBottomBar({ height: 100 }); // Bottom bar's height is changed to 100\n
\n\n

With these changes:

\n\n\n\n\n

3. Statics

\n\n

Static members can be defined using the statics config

\n\n
Ext.define('Computer', {\n    statics: {\n        instanceCount: 0,\n        factory: function(brand) {\n            // 'this' in static methods refer to the class itself\n            return new this({brand: brand});\n        }\n    },\n\n    config: {\n        brand: null\n    },\n\n    constructor: function(config) {\n        this.initConfig(config);\n\n        // the 'self' property of an instance refers to its class\n        this.self.instanceCount ++;\n\n        return this;\n    }\n});\n\nvar dellComputer = Computer.factory('Dell');\nvar appleComputer = Computer.factory('Mac');\n\nalert(appleComputer.getBrand()); // using the auto-generated getter to get the value of a config property. Alerts \"Mac\"\n\nalert(Computer.instanceCount); // Alerts \"2\"\n
\n\n

4. Inheritance

\n\n

Ext JS 4 supports inheritance through both subclassing and mixins. For more information on inheritance see the documentation for Ext.Class

\n\n

6. Dependencies

\n\n

Another new feature introduced in Ext JS 4 is dynamic loading of dependencies. For more information see the documentation for Ext.Loader

\n\n

IV. Errors Handling & Debugging

\n\n
\n\n

Ext JS 4 includes some useful features that will help you with debugging and error handling.

\n\n\n\n\n

\"Call

\n\n

See Also

\n\n\n\n" }); \ No newline at end of file