user3137766
user3137766

Reputation:

Titanium appcelerator clear all global variables when logging out

Any appcelerator titanium developper here :) ? On my app mobile i have a classic login/logout pages, when login i have a global variable "Alloy.Globals.user" created which contains all user objects (name, photo etc ...), i have also some app properties (Ti.app.properties). When a personne logout from the app, i want to destroy all globals variables and properties, actually i did :

$.logout.addEventListener("click",function(e){
    Alloy.Globals.user = {};
    Alloy.Globals.user = null;
    Ti.app.properties.removeProperty("property_name")
})

But if another user log in on the same device, sometimes previous user informations still displayed as new user profile.

How should i handle this please?

Thanks all.

Upvotes: 0

Views: 291

Answers (1)

Rene Pot
Rene Pot

Reputation: 24815

First, don't store anything in globals. It is possible but bad practice. You can use a lib and store the information there.

But that said, resetting the global variable should do the trick. However, your addEventListener doesn't do anything at all. What is it supposed to do?

addEventListener requires 2 properties. The first is which event to listen to and the second the function to call when that specific event fires. Assuming $.logout is a button, you should do $.logout.addEventListener('click',function(){});

Also, if you have displayed the user information in, say, a window. You need to remove that window and clear it from memory. The displayed information isn't removed when you remove the information itself. It basically is a rendering of what you had at the time it rendered. So remove any UI element that displays information and remove all data property when you want the user to logout. That should do the trick.

How to use lib?

lib is a folder inside app. If you don't have it, create it. In that folder you can create files that "do stuff". Variables inside a lib file are stored during the session just like a global variable. Only, it isn't on the global namespace.

lib files are commonjs files. You can include lib files using require('myLibFile') without adding the .js extension. Don't store the result on a Global either, but just require it wherever you need it and the variables will be there.

Upvotes: 1

Related Questions