Results 1 to 10 of 10

Thread: How to define constants file in sencha touch 2.0

  1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    14
    Vote Rating
    0
      0  

    Default How to define constants file in sencha touch 2.0

    Hi,
    I want to make one properties file that will contain all the constants for my application. Can anyone please help me out how to do this in Sencha Touch 2.0 and how to access that file and constants defined in the file in my controllers, views and model. This property file will contain all my application specific constants as we do in Java.

    Thanks,
    Divya

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    Code:
    Ext.define('MyApp.util.Config', {
        singleton : true,
    
        config : {
            foo : 'bar'
        },
    
        constructor: function(config) {
            this.initConfig(config);
    
            this.callParent([config]);
        }
    });
    You need to require the class and put it in app/util/Config.js

    Then anywhere you can use MyApp.util.Config.getFoo()
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #3
    Sencha User
    Join Date
    Apr 2012
    Posts
    6
    Vote Rating
    0
      0  

    Default Require class for constants..

    I tested the solution, but I got everytime: Uncaught TypeError: Cannot read property 'Config' of undefined

    My code was:
    Code:
    Ext.application({
        controllers: [....],
        models: [.....],
        name: 'MyApp',
        views: ['...'],
       stores:['...'],
       .....
        launch: function() {
        Ext.require('MyApp.util.Config');
        ........
        Ext.Ajax.request({
            url:  MyApp.util.Config.getUrlLocalProxy() + '/',
                    ....
    }
    The Config.js file is exactly an copy of the sample.

    Code:
    Ext.define('MyApp.util.Config', {
        singleton : true,
        config : {
            urlLocalProxy: '127.0.0.1/utilityProxy.php'
        },
        constructor: function(config) {
            this.initConfig(config);
            this.callParent([config]);
        }
    });
    Is there any else to do?

    Thanks,

    Daniel

  4. #4
    Sencha User
    Join Date
    Feb 2012
    Posts
    14
    Vote Rating
    0
      0  

    Default

    I have done everything exactly the same way as described by mitchell and I am able to solve my problem.

  5. #5
    Sencha User
    Join Date
    Aug 2011
    Location
    Belgium
    Posts
    29
    Answers
    4
    Vote Rating
    4
      0  

    Default

    I don't require it in launch-function, but in the application itself (and make sure the file is in the right folder: app/util/Config.js):
    Code:
    Ext.application({
        requires: [  'MyApp.util.Config'  ],
        controllers: [....],
        models: [.....],
        name: 'MyApp',
        views: ['...'],
        stores:['...'],
       .....
        launch: function() {
        ........
        Ext.Ajax.request({
            url:  MyApp.util.Config.getUrlLocalProxy() + '/',
                    ....
    }

  6. #6
    Sencha Premium Member
    Join Date
    Nov 2011
    Posts
    24
    Answers
    2
    Vote Rating
    0
      0  

    Default

    Is it possible to use a constants file when your application does not use the MVC pattern... ie....stand alone pages?

  7. #7
    Sencha User
    Join Date
    Aug 2011
    Location
    Belgium
    Posts
    29
    Answers
    4
    Vote Rating
    4
      0  

    Default

    Quote Originally Posted by essex View Post
    Is it possible to use a constants file when your application does not use the MVC pattern... ie....stand alone pages?
    As long as you "require" the config-file with the constants, i think that is possible (if i understand it well).

  8. #8
    Sencha User
    Join Date
    Jun 2012
    Posts
    49
    Answers
    7
    Vote Rating
    8
      0  

    Default

    Code:
    config : {         foo : 'bar'     }
    call by:

    Code:
    ... getFoo() ...
    works, but why? where is the wrapper function from?

  9. #9
    Sencha Premium Member
    Join Date
    Nov 2011
    Posts
    24
    Answers
    2
    Vote Rating
    0
      0  

    Default

    Sencha touch will automagically generate getters and setters for you.

  10. #10
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    The initConfig method call in AbstractComponent's constructor method sets off the getter/setter creation.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •