How to reuse Ext JS models and stores in Sencha Touch 2

After another day of Sencha Touch 2, I discovered that it is a little less compatible with Ext JS then hoped for. Quite annoying is that Ext JS models and stores are not compatible with Sencha Touch 2. And as I don’t want to code them twice when building an Ext JS and Touch application, I looked for a solution and found it.

Another search on the internet helped me to find this article: link. It shows code for making Sencha Touch components compatible with Ext JS. But I am interested in the other way around. So I modified the source code. The original article shows the code in Coffeescript, but here the samples are plain JavaScript. The samples are limited to model and controller.

The compatibility script

Following script has to be loaded prior to application start. This can be done to put it at the top of your app.js, but I recommend to put it a separate JavaScript file and include it in your HTML. The only flaw it the "Touch" test. It will not hold until version 3 arrives. It would be nice when Sencha would add the framework name to the Ext.version object.

The Ext JS model definition

Now we make a slight modification to the Ext JS model. We are going to embed into the global function Sencha.modelCompatibility, which we just created.

The Ext JS store definition

The same we do with the Ext JS store. We embed it into the function Sencha.StoreCompatibility.

By embedding the model and the store the configurations are dynamically converted to Sencha Touch 2 syntax. If you don’t want to convert, you can leave the "Sencha" functions out.

When to use it

  • when you want to use models and stores in Ext JS and Sencha Touch, but only want to have 1 repository.
  • When you are an Ext JS die hard and don’t want to commit to Sencha Touch standards

When not to use it

  • When you use your models and stores only in Sencha Touch 2.
  • When you only develop in Sencha Touch 2.
  • If you want to be really sure. Any coming updates of Sencha Touch or Ext JS could make this small converter incompatible.

Links

CoffeeScript

Convert CoffeeScript to JavaScript

Robust, reusable Sencha Touch and Ext JS code

Johan van de Merwe
Dedicated to professional software development since 1985. Has worked since 1992 as IT manager in several international operating companies. Since 2007 CEO and Sencha Ext JS web application developer at Enovision GmbH.

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.