Monday 1 April 2013

How to define global functions in Sencha Touch 2

We have already seen how to declare global variables in Sencha Touch 2. Today, we are going to see how to define global functions in Sencha Touch 2. Before that, why you need a global functions.

Global functions are defined once and access it anywhere in your application. If you need to modify it, you need to modify it in one place instead of modifying more places of your application.


Usually Global Functions in Sencha Touch 2 is defined inside Ext.application() class.
Ext.application({

    models: [],
    stores: [],
    views: [],
    name: 'MyApp',
    controllers: [],
    launch: function() {},

    getTrimContent: function(content) {
        return Ext.String.trim(content);
    }
});

Here, getTrimContent() is the global function which is used to trim whitespace from either end of a string, leaving spaces within the string intact. In order to access the global function anywhere in your application, you need to use this code.
MyApp.app.getTrimContent(" This is Global Test ");

This one follows the following format {<application name>.<app folder name>.<global function>}.

I Hope, you enjoyed this Post.

4 comments:

  1. Is this still valid in ST 2.2?

    ReplyDelete
    Replies
    1. Yes, it will support Sencha Touch 2.2

      You can also create global function in Utiliy class, Please read this Post
      http://sureshdotariya.blogspot.com/2013/06/how-to-create-utility-class-in-sencha.html

      Hope, its usefull. Thanks

      Delete
    2. Finally! I couldn't figure this out for the life of me (Though I'm brand new to sencha touch and pretty newbish on javascript anyway).

      Thanks!

      Delete
    3. Thanks for your appreciation.

      Suresh Ariya

      Delete