Web Application Architecture with ExtJS

I’ve spoken at great length about the need for developers to have an organized User Interface, and I frequently see questions posted to the ExtJS Sencha message boards asking about “Best Practices” for creating a web application with the ExtJS framework.

As I recently started a new project using ExtJS, I thought I would share some of my own advice on the subject.

Folder Structure

Architects build things… and as software architects, it’s our job to build an organized folder structure as the solid foundation for our web application. Organization is the key here; we don’t want to be searching through our IDE for hours trying to find something. To accomplish this task, we will setup a UI based on “tiers” (think of an N-tier application).

A habit I’ve developed over the years is to create a root-level folder named with an underscore as the first character (/_app/) which contains important UI application files. The underscore is important for two reasons:

  1. it visually stands out, and
  2. it will be listed at the top when sorted alphabetically

For argument’s sake, we will create a folder structure for our application which looks like the following:

Web Application Architecture with ExtJS: Folder Structure

In this way, we keep all of our UI application code separated from other web entities like HTML and image files. (I consider JavaScript and CSS to be “UI application code”, which I define as logic or presentation within the UI. HTML and other file types are generally more structural and are dependent on JS or CSS. Server-side files often contain logic, but fall into a different tier of the application at large.)

If necessary, the folder structure can be much more robust – it completely depends on how your application works. For example, a project running ASP.NET MVC will have root-level folders for Models, Controllers and Views which contain code specific to their tier’s functionality.

ExtJS Framework

The first thing any application using ExtJS will need is the framework itself (duh!). Copy the ExtJS files to /_app/ext-3.2.1/ Name the folder to match whatever version you’re running – this makes future upgrades a breeze.

Assuming that you’re not using ExtJS in conjunction with another framework (like jQuery), your application simply needs to include three files:

  • ext-all.css
  • ext-base.js
  • ext-all.js (or ext-all-debug.js)

I always like to include the CSS file before the JS files, but that’s just my preference. ext-base.js must be included before ext-all.js – read your INCLUDE_ORDER.txt file!

    <!-- ExtJS Framework -->
    <link rel="stylesheet" type="text/css" href="/_app/ext-3.2.1/resources/css/ext-all.css" />
    <script type="text/javascript" src="/_app/ext-3.2.1/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="/_app/ext-3.2.1/ext-all.js"></script>

Themes

If you have any custom ExtJS themes that you plan on using for your application, this would be the appropriate time to add them.

Place these files in the /_app/ext-themes/ folder. We specifically separate these files from /_app/ext-3.2.1/ to simplify the upgrade process should you migrate to a newer version of ExtJS in the future.

    <!-- ExtJS Themes -->
    <link rel="stylesheet" type="text/css" href="/_app/ext-themes/css/xtheme-XYZ.css" />

Overrides

Next, you should create CSS and JS files to place any overrides which you’ll need. These overrides are generally bug fixes – not customizations. The point of these files is to have a central place for fixing oddities in the ExtJS framework which may be fixed in a future release.

Here’s what I call these files:

  • Ext.Overrides.css
  • Ext.Overrides.js
    <!-- ExtJS Overrides -->
    <link rel="stylesheet" type="text/css" href="/_app/ext-overrides/Ext.Overrides.css" />
    <script type="text/javascript" src="/_app/ext-overrides/Ext.Overrides.js"></script>

At this point in our application (the beginning), we probably won’t have any overrides since we haven’t started writing any code. One exception is the global variable BLANK_IMAGE_URL value, which needs to point to our local application (not the remote ExtJS server, which is by default).

In our Ext.Overrides.js file, simply add the following:

    Ext.BLANK_IMAGE_URL = '/_app/ext-3.2.1/resources/images/default/s.gif';

UX Classes

Last but not least, we can now include any ExtJS user extensions (UX) that we need for our application.

    <!-- ExtJS UX -->
    <script type="text/javascript" src="/_app/ext-ux/Ext.ux.XYZ.js"></script>

Application Code

The first thing we should create at this point is our application CSS files (placed in /_app/css/ ). These styles may or may not have anything to do with our JS classes, but we obviously need to include them. I suggest including these styles now (as opposed-to before the ExtJS styles) because we don’t want ExtJS to overwrite any of our custom styles. (Browsers apply styles based on the most recently included definition.)

Next it’s time to add our custom application classes (placed in /_app/js/ ). Although I won’t go into any specific details about how to write your classes, I highly suggest reading the posts by Ed Spencer and Jay Garcia (see below under Resources) which contain excellent tutorials.

Namespace

I usually start by creating a namespace file which asks ExtJS to creates all of my application namespaces before I load my JavaScript classes. This isn’t necessary, but I’ve found that creating the namespaces early often prevents errors due to classes being included in the “wrong order”. It can also save code, as none of your classes need to explicitly create their own namespace.

I call this file _Namespace.js – using the same naming convention as before (starting with an underscore) to draw attention to its importance. It’s contents include only a handful of lines (depending on how large your application is). This file should only contain namespace definitions.

    Ext.namespace(
        'NS1',
        'NS2',
        'NS3'
    );

Also, if you’re going to use this idea, be sure to include _Namespace.js before any of your other application files!

JS Classes

Lastly, we can include all of our application’s JavaScript classes in the HTML header.

    <!-- Application Files -->
    <link rel="stylesheet" type="text/css" href="/_app/css/styles.css" />
    <script type="text/javascript" src="/_app/js/_Namespace.js"></script>
    <script type="text/javascript" src="/_app/js/MyClass1.js"></script>
    <script type="text/javascript" src="/_app/js/MyClass2.js"></script>
    <script type="text/javascript" src="/_app/js/MyClass3.js"></script>

Put It All Together

    <!-- ExtJS Framework -->
    <link rel="stylesheet" type="text/css" href="/_app/ext-3.2.1/resources/css/ext-all.css" />
    <script type="text/javascript" src="/_app/ext-3.2.1/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="/_app/ext-3.2.1/ext-all.js"></script>

    <!-- ExtJS Themes -->
    <link rel="stylesheet" type="text/css" href="/_app/ext-themes/css/xtheme-XYZ.css" />

    <!-- ExtJS Overrides -->
    <link rel="stylesheet" type="text/css" href="/_app/ext-overrides/Ext.Overrides.css" />
    <script type="text/javascript" src="/_app/ext-overrides/Ext.Overrides.js"></script>

    <!-- ExtJS UX -->
    <script type="text/javascript" src="/_app/ext-ux/Ext.ux.XYZ.js"></script>

    <!-- Application Files -->
    <link rel="stylesheet" type="text/css" href="/_app/css/styles.css" />
    <script type="text/javascript" src="/_app/js/_Namespace.js"></script>
    <script type="text/javascript" src="/_app/js/MyClass1.js"></script>
    <script type="text/javascript" src="/_app/js/MyClass2.js"></script>
    <script type="text/javascript" src="/_app/js/MyClass3.js"></script>

Optimization

Larger applications may have hundreds of JavaScript files containing custom classes, methods, etc. Simply including each file in the HTML header will result in potentially long page load times – and this is far from ideal.

Many web servers compress JS files for you, and you always have the option of minifying your code – but loading hundreds of compressed and minified files still takes time and bandwidth.

One option is to combine/compress all of your classes into a single file – exactly as ExtJS has done with their ext-all.js file. This is a great solution – but it’s a manual process, one which can be tedious and is prone to user-error.

A far better solution is to automate this process – and if you’re using Visual Studio (like me), Dave Ward has a great post on setting that up.

Lastly, tools such as YSlow and Google Page Speed will suggest adding your JavaScript files to the bottom of the page. This may be advantageous; however, if your application makes use of the ExtJS Viewport or the famous Ext Desktop example, it probably won’t do much good as the UI is completely dependent on the JS framework.

Your Thoughts

Do you have any thoughts on what I’ve described above? Are you doing something that I missed?

I look forward to seeing your comments!

Resources

Share and Enjoy:
  • RSS
  • Facebook
  • StumbleUpon
  • Digg
  • Sphinn
  • Technorati
  • Reddit
  • LinkedIn
  • Twitter
  • Tumblr

About Arthur Kay

Arthur Kay is a long-time nerd and JavaScript enthusiast. He lives in the Chicago suburbs and is active in the local web development community. Arthur currently works for Sencha, Inc. as a Solutions Engineer. The thoughts, ideas, and opinions expressed on this website are Arthur's alone and do not represent his employer.
This entry was posted in ExtJs. Bookmark the permalink.

7 Responses to Web Application Architecture with ExtJS

  1. Sanel says:

    Hi Arthur

    This is very useful post. It will help me a lot. Thanks

  2. Victor Cardins says:

    Hi Arthur,

    Thanks a ton ! This post was my start point for building my first Ext professional application. I still have a concern. I have used a MVC php framework – DooPhp and I was wondering if instead the _apps/js folder, the presentstion javascript shouldn’t be within the view folder ?

    root
    + global
    + css
    + ext
    + js ( common js)
    + controller
    + model
    + view
    + user ( JSs for presenting User related information)
    + order ( JSs for presenting Order related information)

    I appreciate your attention !

  3. Arthur Kay says:

    Victor,

    That’s a good question, and I don’t really have a definitive answer for you.

    My understanding of “Views” in MVC is that they’re bound to the “Controllers”. Thus if you had a “Home” controller, you might have an “Index” view at /Home/Index.

    Because our JavaScript code isn’t bound to the controllers I (personally) wouldn’t put the JS inside the “Views” folder. Views also typically contain server-side code (in your case, PHP) so separating server-side and client-side functionality makes sense to me.

    On the other hand, Views do contain presentation functionality – so you could put your JS code in a Views/Shared folder.

    Another concern I would have relates to “Areas” in MVC (I use ASP.NET MVC, so I can’t speak specifically about DooPHP). Because we might have many “Areas” in our application, we may want all of our JS code globally accessible. In that case, it wouldn’t make sense to have our JavaScript code inside a “Views” folder because those views are specific to the containing area.

    I suppose it’s all a matter of preference.

  4. Kevin says:

    Using asp.net mvc how many Ext “apps” do you have? i.e. is there just one for everything or one for each controller or logical grouping?

    • Arthur Kay says:

      I would probably only have one “_app” for most projects, but it’s possible you could have multiple interfaces using the same backend.

      For example, you might have a desktop UI and a mobile UI… others are possible as well.

      Lastly, the “_app” is the encompassing UI, which interacts with any number of your controllers. Does that answer your question?

  5. Kevin says:

    So I have customers, purchase orders, ar, ap, etc. then all are under _app. There never will be an “index” or page rendered by any controller (other than the start), just json correct?

  6. Arthur Kay says:

    It completely depends on your application.

    If you are using a JS framework like ExtJS, it is unlikely you’ll ever have a true “View” rendered by a controller because the framework is used to build the UI components.

    On the other hand, you might have situations where you actually want a View (or Partial View) rendered directly into the DOM.

    In most of my ExtJS and Sencha Touch applications, I only use the “Index” view to launch my JS code (for example, creating an ExtJS Viewport to encompass the UI), also possibly injecting preliminary data via ASP.NET (or PHP, or whatever).

    Even though we may be using an MVC approach to our backend, our AJAX applications won’t rely on traditional MVC Views.

    ExtJS 4.0 actually takes this idea to the next level. If you haven’t done so already, I suggest reading the MVC guide in the ExtJS 4.0 documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>