1. #1
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Answers
    1
    Vote Rating
    -2
    Ricardo Perre has a little shameless behaviour in the past

      0  

    Default Answered: Sencha Touch 2.X Global Variables - Everywhere access

    Answered: Sencha Touch 2.X Global Variables - Everywhere access


    I know this question was already posted here but I either didnt understand or sencha changed somewhat.


    My app loads a form panel for login, then I would like to save the user info that have just loged on. This way I can change my view anytime I want and still know the name of the loged in user.


    Here is my main code:


    Code:
    //<debug>
    Ext.Loader.setPath({
        'Ext': 'sdk/src'
    });
    //</debug>
    
    
    Ext.application({
        name: 'APP',
    
    
        loadedUser: 'the test',
    
    
    
    
        requires: [ 'Ext.MessageBox' ],
    
    
        views: ['Main', 'Home', 'Login'],
    
    
        models: ['User'],
    
    
        stores: ['Users'],
    
    
        controllers: ['Main'],
    
    
        icon: {
            57: 'resources/icons/Icon.png',
            72: 'resources/icons/Icon~ipad.png',
            114: 'resources/icons/Icon@2x.png',
            144: 'resources/icons/Icon~ipad@2x.png'
        },
    
        phoneStartupScreen: 'resources/loading/Homescreen.jpg',
        tabletStartupScreen: 'resources/loading/Homescreen~ipad.jpg',
    
    
        setLoadedUser: function(arg) {
            this.loadedUser = arg;
        },
    
    
    
    
        launch: function() {
    // Destroy the #appLoadingIndicator element
            Ext.fly('appLoadingIndicator').destroy();
    
    
    // Initialize the main view
            Ext.Viewport.add(Ext.create('APP.view.Main'));
        },
    
    
        onUpdated: function() {
            Ext.Msg.confirm(
                "Application Update",
                "This application has just successfully been updated to the latest version. Reload now?",
                function() {
                    window.location.reload();
                }
            );
        }
    });

    The 'loadedUser' its what I wanted to be my global variable, and the method setLoadedUser(arg) its suposed to change that value.


    I can access 'loadedUser' no problem, but i cant change its value.
    Another question: loadedUser can it be an array/data structure?


    Many thanks
    Last edited by mitchellsimoens; 27 Mar 2012 at 5:16 AM. Reason: formatted code

  2. this = scope

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,329
    Answers
    3541
    Vote Rating
    847
    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


    If you can access it you can change it. Nothing in JavaScript is private.
    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.

  4. #3
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Answers
    1
    Vote Rating
    -2
    Ricardo Perre has a little shameless behaviour in the past

      0  

    Default


    Code:
    Ext.define("APP.view.Main", {    extend: 'Ext.tab.Panel',
        xtype: 'mainview',
        itemId: 'mView',
        loadedUser: 'the test',
        
        config: {
            activeItem: 1,
            items: [
                {
                    xtype: 'homepanel'
                },
                {
                    xtype: 'loginpanel'
                }
    
    
            ]
        },
    
    
        getLogedInUser: function() {
            return this.loadedUser;
        },
        setLogedInUser: function(arg) {
            this.loadedUser = arg;
        },
    
    
        listeners: {
            show: function(list, opts) {
                this.getTabBar().hide();
            }
        }
    });

    When I do this
    Code:
    Ext.Viewport.getComponent('mView').setLogedInUser('new val');
    And then
    Code:
    Ext.Viewport.getComponent('mView').getLogedInUser()
    the value its still 'the test'

    What am I doing wrong? Thanks for your time
    Last edited by Ricardo Perre; 27 Mar 2012 at 8:15 AM. Reason: typo

  5. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,329
    Answers
    3541
    Vote Rating
    847
    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


    What is the scope within the setLogedInUser?
    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.

  6. #5
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Answers
    1
    Vote Rating
    -2
    Ricardo Perre has a little shameless behaviour in the past

      0  

    Default


    Didnt quite got you question.
    By scope I understand the namespace.
    The namespace of my application is APP (like in the original post) then I instantiate two views (home and login). I'm calling this function in this two views.

    Sorry for not knowing exactly what is the scope.

  7. #6
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,329
    Answers
    3541
    Vote Rating
    847
    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


    this = scope
    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.

  8. #7
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Answers
    1
    Vote Rating
    -2
    Ricardo Perre has a little shameless behaviour in the past

      0  

    Default


    The variable and the two methods are in the MainView. Its a tab panel that contains all the other views and I set the activeItem of the one I want to show at a particular time.

    MainView {
    -> has the methods and the variable

    { //login view
    -> I call the methods

    }
    { //another view
    -> I again call the same methods
    }

    }

  9. #8
    Sencha User
    Join Date
    Mar 2012
    Posts
    23
    Answers
    1
    Vote Rating
    -2
    Ricardo Perre has a little shameless behaviour in the past

      -1  

    Default


    Solved it. Thanks.

  10. #9
    Sencha User
    Join Date
    Dec 2012
    Posts
    1
    Vote Rating
    0
    cliffcloudy is on a distinguished road

      0  

    Default


    Quote Originally Posted by Ricardo Perre View Post
    Solved it. Thanks.
    got the same problem here..

    may I ask how do you solve it ?