-
27 Mar 2012 3:09 AM #1
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 thanksLast edited by mitchellsimoens; 27 Mar 2012 at 5:16 AM. Reason: formatted code
-
Best Answer Posted by mitchellsimoens
this = scope
-
27 Mar 2012 5:20 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
If you can access it you can change it. Nothing in JavaScript is private.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
27 Mar 2012 8:08 AM #3
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 thisAnd thenCode:Ext.Viewport.getComponent('mView').setLogedInUser('new val');the value its still 'the test'Code:Ext.Viewport.getComponent('mView').getLogedInUser()
What am I doing wrong? Thanks for your timeLast edited by Ricardo Perre; 27 Mar 2012 at 8:15 AM. Reason: typo
-
27 Mar 2012 8:23 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
What is the scope within the setLogedInUser?
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
27 Mar 2012 8:26 AM #5
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.
-
27 Mar 2012 8:31 AM #6Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
this = scope
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
27 Mar 2012 8:33 AM #7
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
}
}
-
28 Mar 2012 1:19 AM #8
-
20 Dec 2012 7:30 PM #9


Reply With Quote