1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    14
    Vote Rating
    0
    divya_singh is on a distinguished road

      0  

    Default Unanswered: How to define constants file in sencha touch 2.0

    Unanswered: 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 - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,548
    Vote Rating
    872
    Answers
    3566
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      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 @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Apr 2012
    Posts
    6
    Vote Rating
    0
    danielpizarro is on a distinguished road

      0  

    Default Require class for constants..

    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
    divya_singh is on a distinguished road

      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
    Vote Rating
    4
    Answers
    4
    gamevampy is on a distinguished road

      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
    Vote Rating
    0
    Answers
    2
    essex is on a distinguished road

      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
    Vote Rating
    4
    Answers
    4
    gamevampy is on a distinguished road

      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
    Vote Rating
    5
    Answers
    7
    Malro is on a distinguished road

      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
    Vote Rating
    0
    Answers
    2
    essex is on a distinguished road

      0  

    Default


    Sencha touch will automagically generate getters and setters for you.

  10. #10
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,548
    Vote Rating
    872
    Answers
    3566
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    The initConfig method call in AbstractComponent's constructor method sets off the getter/setter creation.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.