Writing a Big Application in Ext (Part 2)

Code in this post can be obsolete, however, principles and theory may still apply.

Important

If you have not already done so, study Writing a Big Application in Ext (Part 1) before you read this article. It would be very hard, if not impossible, to understand concepts explained here before you fully understand the first part.

Introduction

It has been almost one year since I have published the first part of this article. I have been successfully using the concept of pre-configured classes personally to write a really big application (~150 javascript files, ~60,000 lines of code, plus server-side and css). The application is fully modularized, each class in separate file and it has proven that it can be easily developed, managed and debugged.

Unfortunately, the same has not been always true for other users, they were hitting various problems and Ext Support Team have had to handle may questions and help requests. These accumulated to the degree where the concept has been called “absolute abomination” (read absolutely hated) and it was stated that “it causes problems”.

Note: As “fast cars” do not cause accidents but “slow drivers driving fast cars” do, the concept itself cannot be a cause of the problems but its application can.

In any case, there must be some illogic if I can use the concept but others cannot.

Thus, I have looked deeper in it and I have isolated some pitfalls the users can run into on the course of development. I will also write on “dos” and “don’ts” and on when to use a pre-configured class and when not. I will not compare this concept to another application design concepts (factory functions, in-line configuration, on-demand load, or other) because 1) I do not use them and 2) I do not want to turn this article into a Linux versus Windows discussion.

It is fully up to you which application design concept you choose. However, if you do choose this one then follow the rules I’m going to lay down.

Most Common Sources of Troubles

  • Dull Copy&Paste
  • Extending a non-Ext.Component class
  • Not calling parent methods
  • initialConfig
  • listeners

Dull Copy&Paste

Do you know such people? They post on a forum:

I need to drag from tree to qrid, gimme plz complete codez

And if somebody altruistic writes a fragment of “codez” for him in a sheer attempt to help the response is going to be:

Your codez don’t work. Help me plz my manager wants it working

Do you see what happened? A dull “developer” ordered a code on the forum, he’s got some, copied&pasted it to his application without a clue what the code does, maybe hasn’t even changed url that still points to your server and the result is: it doesn’t work.

Well, this is an extreme (but not so rare as you would think), nevertheless, copying&pasting without understanding of what the copied&pasted code does can lead only to frustrations.

I am not against Copy&Paste in general, it can save a lot of time and I also occasionally do it, but I am against not-understanding or mis-understanding not only of coding but also of life.

The Rule: Do Copy&Paste but always with full understanding of what the code does.

Extending a non-Ext.Component class

If an Ext class does not inherit from Ext.Component the initComponent is never called so the code you have written there is never executed. This is fragment from Ext.Component constructor:

Ext classes that do not inherit from Ext.Component do not have this.initComponent(); line in their constructors.

The Rule: Always check if the Ext class you are going to pre-configure inherits from Ext.Component. You have to use an another approach if it does not.

Not calling parent methods

It happens very often that you do not only add some methods in your extended class but that you modify existing ones. initComponent being the first example. onRender, afterLayout are other (but not only) frequently overriden methods.

These methods are already implemented in the class you are extending and its parents so if you forget the line:

your class will not work.

The Rule: Never forget to call the parent method, unless you exactly know what you are doing.

initialConfig

The constructor of Ext.Component saves the config passed to it as initialConfig:

You see what happens? The constructor saves initialConfig before initComponent is executed. Thus, all configuration you write in initComponent is not saved. I have overlooked this in first versions of my templates and examples mainly because there is only a couple of classes that refer to initialConfig and even in these classes the absence of properly saved initialConfig causes problems very rarely. These Ext classes refer to initialConfig:

  • AnchorLayout
  • BorderLayout
  • Action
  • GridPanel
  • Tip
  • Combo
  • Form

Now, I have updated all my examples, extensions, templates and main site to include this “magic” pattern:

The Rule: Ensure that your extension saves initialConfig.

listeners

If you try to install event handlers by setting property listeners in your config they will not work. Why? The answer lies again in the order of actions in Ext.Component constructor:

As you can see, the constructor calls its parent, that is Ext.util.Observable before initComponent. Ext.util.Observable constructor executes:

Any listeners set in initComponent are thus ignored.

There is an easy workaround. Put constructor method in your extension:

and define your listeners therein.

The Rule: Define listeners in constructor method.

Conclusion

If you decide to use my way of writing a big application then follow these rules:

  1. Do Copy&Paste but always with full understanding of what the code does.
  2. Always check if the Ext class you are going to pre-configure inherits from Ext.Component. You have to use an another approach if it does not.
  3. Never forget to call the parent method, unless you exactly know what you are doing.
  4. Ensure that your extension saves initialConfig.
  5. Define listeners in constructor method.

Happy extending!

Do not forget to read Part 1 and Part 3 of this article.

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Email this to someone
Follow me:

Saki

I'm a well seasoned developer, consultant and educator of web applications based mainly on Sencha libraries, PHP, MySQL and Node.js. Besides (Apple) computers, I love photography and mountain biking.
Follow me:

Latest posts by Saki (see all)

Comments

  1. says

    Saki, I want to share my experience here.

    Initially, I’m a Flex developer, but Big Client came to our company asking for ExtJS expert for developing Outlook+Gmail+IM alternative to be used in their intranet as info sharing application.

    We agreed, I’ve heard about ExtJS and even run a couple of samples year ago.

    And then project started, I was very quickly learning ExtJS and was shooting my legs again and again, because all the JavaScript and JSON are so hacky for the ActionScript3 developer, who have compiler for all error checking, strict typing, and all static language comfort.

    Important moment, we use SCRUM for the development, and very tight deadlines.

    And then I’ve discovered your patterns and learned a lot in one moment. I can’t say it prevented me from making new errors, but your approach cleared a lot of questions, so now 2 months of development are gone, and I can say application is more or less stable, could be extended and supported further.

    I want to thank you for the great work you’ve done, and all the inspiration of your samples.

    Regarding pitfalls, yes, I had it once, the one with listeners, which lead me to setting listeners in “onShow” method, but then I looked into your greatly documented code again and found my mistake.

    Thank you!

  2. says

    @Rostislav,

    thank you very much for sharing your practical life experiences with extended classes here. I wish you good luck with them in the future.

  3. Bill Hutten says

    Saki: Thanks very much for these clarifications. We\’re working on a medium-sized ExtJS application (~14K lines right now, about 1/2 done), and are using pre-configured classes extensively. They\’ve worked very well for us.

  4. Relexx says

    Thanks for this Saki. I am a complete convert to xtype and lazy instantiation. It makes my code more readable and easier to configure.

  5. Gjslick says

    Hey Saki, thanks for this great article. It really got me going on extending Ext’s classes.

    I just wanted to give an update about the last section in this post about listeners. As of (I believe) Ext 3.2.0, you can now add a listeners property inside of initComponent(). This is due to Component.initComponent() duplicating Observable’s constructor for this very purpose (probably from so many people writing their own pre-configured classes!)

  6. says

    Hi. Nice work, that helpme much.

    So, I have a question.

    How can I call a parent listener (like keypress) when that listener has be overwritten?

    Thanks.

We will be happy to hear back from you

Please Login to post a comment