SwarmOnline Menu Mobile
  • Home
  • About
  • Services
  • Portfolio
  • Blog
  • Contact

Recent Posts

  • PhoneGap and the TestFlight SDK – Taking it Further!
  • Integrating the TestFlight SDK into your PhoneGap Application
  • Retrieving References to Internal View Components
  • Launching Sencha Insights
  • Documenting your Sencha apps with JSDuck

Recent Comments

  • Stuart on Ext.ux.TouchCalendar – A Sencha Touch Calendar component
  • Stuart on Localising Sencha Touch and Ext JS applications with Ux.locale.Manager
  • Manuel Rodriguez on Localising Sencha Touch and Ext JS applications with Ux.locale.Manager
  • Remi Bloch on Ext.ux.TouchCalendar – A Sencha Touch Calendar component
  • Remi Bloch on Ext.ux.TouchCalendar – A Sencha Touch Calendar component

Archives

  • May 2013
  • March 2013
  • February 2013
  • December 2012
  • September 2012
  • August 2012
  • November 2011
  • October 2011
  • May 2011
  • March 2011
  • February 2011
  • January 2011
  • November 2010

Categories

  • Development Tips
  • Ext JS
  • News
  • Sencha Touch
  • Sencha Touch Extensions & Plugins
  • Web Design
  • Web Design & Development Blog

20 Things To Avoid/Do When Getting Started With Ext JS & Sencha Touch

19
04 May
2011

Development Tips

Ext JS

Web Design & Development Blog

When learning a new topic people often make the same mistakes and follow the same bad practices as each other. It usually follows by them realising that what they’ve been doing for a few days/weeks/months is a bad way of doing it and wondering how they didn’t realise earlier.

In this article we’ve put together a (non-exhausted!) list of a few of the things we’ve picked up and things we recommend you do when starting out with Ext JS (hey, even you veteran developers might learn something new!). These things have come up through our own experience; through seeing and replying to the same issues on the forums again and again; and just general good programming practices.

Hopefully this will jump you a few rungs up the ladder of learning Ext JS and help you avoid falling into the usual traps!

Although we are only explicitly talking about Ext JS, most, if not all, of these tips can be applied to Sencha Touch as the structure of the framework is so similar.

Avoid using Ext.getCmp

Don’t be tempted to give all your components IDs and then use Ext.getCmp to find them again. If you structure your applications correctly and apply the principles of Object Oriented (OO) design properly then you shouldn’t have a problem getting access to your components when you need them.

Useful References:

Writing a Big Application by Saki

Component Communication Example by Saki

Avoid Using the ownerCt Property

Don’t rely on the ownerCt property to get a component’s parent. Chances are if you’re wanting to access the component’s parent within its code then the code shouldn’t be there. The code should likely be in the parent itself. This is a similar thing to using Ext.getCmp (above) so make sure you read the linked articles!

Do Not Overnest Components

A hugely common mistake found in new Ext JS developers’ code is the overnesting of Panels. Think carefully about your structure and remember that components such as FormPanels, GridPanels and TabPanels all extend the Panel class so don’t need to be wrapped in another Panel. They can be placed wherever their more generic parents can and be configured with the same options.

Be Careful of Spaghetti Code/Scope

Don’t define all your components in one big file or all inline – it will be a nightmare to debug and your scope will be all over the place. Make things easy and split each component definition into its own file and extend the base class to add your configuration and extra methods and behaviours. This way you can simply instantiate these components where you need them keeping things nice and tidy and loosely coupled.

If you don’t want to extend base classes all the time then using Factory Methods is another option.

Make Sure You Use Namespaces

Again, its all about organisation. Utilise the inbuilt namespace capabilities of Ext JS to organise your components and classes so its easy to navigate later. Typically you would want to do this by functional module, for example, MyApp.Users for all the User management code. I like to mirror this namespace structure with the folder structure so everything is dead easy to find.

Use Get/Set Methods or Static References

Don’t be tempted to delve into a component’s items or toolbar collections to get a reference to a particular button or panel using the ‘comp.get(0)’ or comp.getTopToolbar().get(1)’ syntax. If you ever add items to these collections the references will be broken and you will have to track them down. In a big application this becomes a mammoth task which will introduce bugs.

Save yourself some hassle and, in the extended class, create a get method for the necessary components, which delves into the collection, so when you do add more items you know there is only one method to be updated. Or, if you aren’t concerned about lazy instantiation, define your buttons and items as class variables and reference them that way so you won’t need to make any updates when new items are added.

Name Your xtypes Carefully

Name your xtypes the same as your class, including namespaces, so you can track down the actual class definition very easily.

Reuse Your Code

If you find yourself writing the same code again and again (think masking elements while loading, response/error handling code etc) then wrap that stuff in a function in a way that makes it generic and reuse it. Save yourself the time, effort and RSI – you’ll thank yourself when you decide to change it and have to change hundreds of copies.

Try Not to Nest Functions

Don’t define functions within functions (within functions…within functions…). You may think your doing some good code reuse but your just creating a scoping and organisation nightmare. Trying to follow that sort of nesting is not pleasant, so spare a thought for your co-workers who might have to debug it.

Learn how to use FireBug

…or WebKit’s Developer tools. Make sure you install and use an in-browser tool like FireBug to debug your Javascript and to explore what HTML and CSS the framework is creating. It’s easy to forget that, at the ground level, all Ext is doing is generating markup and these tools will give you an insight into what’s happening in the black box. If you learn how to use these tools properly and how to debug your JS effectively you will save yourself a huge amount of time.

Not to mention everything else it can do – Monitor AJAX requests, DOM exploration, real-time CSS changes etc etc

Make it a White Box!

As Evan says “Don’t be afraid of the source code!”. Use your new found FireBug knowledge to break into the Ext JS Framework rather than always stepping over those calls. It’s incredible how much you can learn and how easily you can find a problem by stepping into a framework method, not to mention the hours of stress trying to figure out a problem.

Always Have the Docs Open

The API documentation is your best friend – it should be your first port of call when running into problems or when trying something new. Intellisense for Ext JS isn’t quite up to the standards of Visual Studio for .NET so we can’t get away with being so lazy – the docs are the answer to this. They are well laid out and easy to navigate, so make use of them!

Debugging Chain

How you should go about fixing a problem…

Check your Code (it’s easy to misspell config options, miss commas etc)

Refer to the Documentation (you might be missing a related config option)

Step into the Framework’s Source Code (break into the code around the error and try to see what’s going wrong, you’ll likely discover a config option you’ve missed, or realise something you expected to be there isn’t instantiated yet)

Search the Forum (it’s almost a guarantee that someone will have had the same problem and posted a question about it)

Search the wider WWW (if the forum can’t help the wider web might be able to)

Post on the Forum (if you still can’t find the resolution, create a new thread with as much detail about the problem as you can – make it easy for people to help you)

Build it Up Gradually

It’s all too easy to get too excited about a project and dive right in and create half a dozen components and then open the browser and give it a try. If it doesn’t work it can be very difficult to move backwards trying to find the culprit. We suggest creating things a stage at a time and gradually build it up once you’re sure each thing works as you want. For example, if you’re trying to create DataView that allows new items to be added to it with the new item being animated in – get the new items adding first then move on to animating it. If you try to do everything at once something’s likely to go wrong.

Minify & GZip your Production Code

We recommend separating out all of your components into their own files. This means you’re going to have a bucket load of requests to download them all so make sure you minify your scripts into a single file to minimise the number of requests and to compress the file size.

Once you’ve done this make sure your server is set up to GZip your scripts. This can reduce the download size by about 70%!

Don’t Edit the Framework Code

Yes, delve into it, read it, test it, learn from it – but don’t edit it! If you start making changes to the core library, whether it is the JS or CSS, you are planting a ticking time-bomb into your application. When you come to upgrade to a newer version all those changes will be lost and you will have to traipse through trying to figure out where things are broken!

If you want to alter the behaviour/look of the framework then go for it but do it by overriding the class (be it a JS class or a CSS class) or method in a seperate script file. By doing this you can remove it if necessary and keep track of it when it comes to reviewing those changes when new versions are released.

Use Plugins and User Extensions

If you are wanting a component to do something that is fairly generic and a common functional requirement it is likely that someone has already coded it and released it as a Plugin or an Extension. Take some time before coding it from scratch (unless you have the time – it’s a great way to learn!) to search the forums and the web for a pre-made solution.

Define Generic Items in a Central Location

It’s handy to define all your variables, namespaces etc that are used application wide in a single config file. This keeps everything in one place, makes values easier to update and encourages reuse.

Consider Using Ext Direct

Ext.Direct is a brilliant feature of the Ext JS Framework and we highly recommend you implement it from the outset. It has various advantages including increasing speed, reducing code and a simplified way of interacting with your server. If you think you might consider it in the future, don’t, consider it now. It’s far easier to implement from the outset than convert everything over. You’ll thank us in the long run!

Test on All Browsers!

Although Ext JS takes care of 99% of cross browser issues always remember to test your application across all the browsers – especially IE! The usual IE CSS issues are common when creating custom data views and JS errors such as rogue commas will bring your application to its knees.

If you have any of your own tips or best practises please leave us a comment and we’ll add them to the list!

Tweet

19
  1. Fatih - Reply

    June 6, 2011 at 11:22 pm

    Really great article that touches most important points. Especially the most common mistake which is overnesting.

    Thank..

    • Andy - Reply

      June 7, 2011 at 2:14 pm

      Thanks for the feedback.

      Pleased you enjoyed it :)

  2. Ian - Reply

    July 1, 2011 at 9:34 pm

    Just to add, stop using bodyStyle & things of that nature to customize the look of your app. ExtJs & Sencha Touch provide SASS/Compass support that literally takes no time to understand but can dramatically up your killing power when it comes to styling your applications.

  3. Scott - Reply

    July 8, 2011 at 3:34 am

    Good article. It’s good to mention the use of JSLint or the like to debug your code quicker. It’s good for finding those pesky missing comma mistakes.

  4. aditia - Reply

    July 23, 2011 at 6:01 am

    great point, but for now sencha touch only support webkit browser

  5. Ilya - Reply

    September 14, 2011 at 3:39 pm

    Great article!
    @Ian good tip
    @aditia Does it really need to support other browsers?

  6. Dawesi - Reply

    September 20, 2011 at 12:25 am

    Some good points. Well done.

    As I’ve argued with Jay for years, I suggest don’t overuse Ext.getCmp(), rather than a blanked ban. It’s elitest to say never, and over the years there’s been numerous people in various programming communities who say ‘never’ to something, only to shown a million legitimate uses for it. I’m yet to find someone who can prove every use case for Ext.getCmp() s not valid. Sometimes you don’t want to polute your object tree with throwaway examples.

    Except for long chains of them, I disagree with your point about ownerCt. There’s plenty of very valid reasons for using ownerCt especially when you’re talking extensions/plugins and re-witing the containers.

    I like the concept of Ext.direct. Out of the box it exposes far too many methods to be secure for anything but an insecure web app, so you have to write your own stack for it to make it this way.

    —

    @Ilya – opera mobile for starters (the most used browser on non-touch devices and the original mobile web browser), ie for windows phone (gartner sees it as #1 by 2015), firefox for mobile, etc… There’s still waaaay more non-webkit phones surfing the web ‘worldwide’. If you stick to the USA stats, then us people in other countries (the other 90% of web users) get pretty upset at your shortsightedness. This is why a lot of people use jQuery (and other js frameworks) for mobile as it’s not as near sighted (and limited) as Sencha Touch in it’s browser implementations.

    • Stuart - Reply

      September 20, 2011 at 12:37 pm

      You’re right, there are occasional legitimate uses for getCmp but I think the important thing in the article was to try and get people’s default thinking away from using it. Once you do that then better architecture decisions are made because the quick solution of getCmp is removed and so applications tend become more structured and consistent.

      The point concerning ownerCt is about coupling components as loosely as possible. By referencing a parent using it means that child component can’t be reused without that specific parent or a parent that shares the same interface existing. As always, as you say, there are exceptions to the rule and if you know the component is unlikely to be reused elsewhere then it isn’t a problem.

      Thanks for the feedback, and I’m glad you found the article interesting!

      Stuart

  7. alien3d - Reply

    September 21, 2011 at 3:04 am

    Ext.getCmp().getValue() is very important to me.. i don ‘t want any classes to multiply extend……………. and more easier to debug……. one object one extends. We not building one simple page program but hundred of functionality together.For those old javascript .document.getelementbyid is eq to me.

    If you extends it,it more harder to debug code..

  8. Rothen - Reply

    September 27, 2011 at 7:04 am

    very cool… I am learning Ext jS nOW…

  9. Ken - Reply

    November 2, 2011 at 8:16 pm

    “Don’t be tempted to delve into a component’s items or toolbar collections to get a reference to a particular button or panel using the ‘comp.get(0)”

    Actually, panel.items.get(0) might be preferred when searching by position, because if you use getComponent and there are no components found in the items list, it will then go and find you a dockedItem. This means you actually could get a docked item where you would expect an error (array out of bounds).

  10. Gary - Reply

    January 18, 2012 at 3:29 pm

    “Name your xtypes the same as your class, including namespaces, so you can track down the actual class definition very easily.”

    I found that if you include extra full stops in your xtype it prevents findParentByType from correctly identifying the record. Do you have an example of your full xtype

    • Stuart - Reply

      January 18, 2012 at 3:44 pm

      Unfortunately xtypes with dots in them don’t work directly in the Ext.ComponentQuery class as the single word xtypes do (e.g. component, dataview etc). You can get around this by adding the xtype as a property comparison within your query.

      For example, if you had a component (“currentComponent”) who had a parent with an xtype of “myNamespace.MyPanel” then you could write that as:

      currentComponent.findParentByType('[xtype="myNamespace.MyPanel"]')

      This effectively says “look for classes with an xtype property equal to ‘myNamespace.MyPanel’”.

      Hope this helps.

      Stuart

  11. Gary - Reply

    January 20, 2012 at 11:33 am

    Excellent. Thanks for that.

  12. Rahul Akula - Reply

    February 1, 2012 at 6:40 am

    Great tips for a newbie like me… :)
    thanks

  13. sonder - Reply

    March 27, 2012 at 4:48 am

    Given Sencha’s limited examples (MVC in particular), cryptic documentation and generally poor forum support (Animal practically flaming people with his cut and paste “RTFM” replies), I say get this stuff working by any means necessary.

  14. Pingback: EXTJS Linkage « TechnoBuzz

  15. Jay Garcia - Reply

    September 4, 2012 at 2:45 pm

    My patterns have evolved over the years from working with lots of companies all around the world in just about all industries. I still say avoid Ext.getCmp! It’s bad practice and people will use it as a crutch.

    You can disagree with me, and I’m OK with it. My apps are developed to be extensible, maintainable, debuggable and generally self-documented.

    I love it how people call me out without me being mentioned in the article! Shows awesome character!

  16. Vincent - Reply

    December 19, 2012 at 3:50 pm

    Nice tip!
    Thank you

Leave a Comment - Cancel reply

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>

About SwarmOnline

Based in Glasgow’s West End, SwarmOnline provide web development, cross platform mobile app development and training services for local and international organisations. We have expert knowledge of Sencha technologies and have been Sencha Partners since 2011. We specialise in creating dynamic, innovative and practical solutions.

Meet the Team...

Newsletter

Subscribe to our newsletter and we’ll keep you up-to-date. We don't do spam.

Get in touch!

Email: [email protected]   
Skype: andrew-swarmonline stuart-swarmonline   
Phone: 0141 438 2231   


© 2013 SwarmOnline Ltd. All Rights Reserved.

SwarmOnline Ltd is a Limited Company registered in Scotland, with company number SC411633. Our Registered Office is 1-2 249 Byres Road, Glasgow G12 8UB.