Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Resetting a collection on logout

$
0
0

I have a fairly simple app; on logging in a model instance is created to store the user info (id, name, etc.). If they opted to be remembered, a property is set. After logging in, a new controller is created which pulls data from the server using the user's id. The collection in this controller is defined in the XML and pulled into the JS by assigning it to a global:

// XML
<Collection src="project" />
 
// JS
Alloy.Globals.projectList = Alloy.Collections.project;
I didn't originally put it into a global, but it seemed like I needed to anyway due to scope restrictions.

Currently, when I log out and log in as another user, it combines the new user's data with the old user's data. In other words, it retains the data in the Collection which should have been reset. I have confirmed that the server is returning the correct data in every case.

My logout function, which is called in a separate controller to the one in question, which is why I set it as a global, resets the collections, deletes the properties and closes the controller:

function logout(e) {
    if(Alloy.Globals.projectList) Alloy.Globals.projectList.reset();
    if(Alloy.Globals.messageList) Alloy.Globals.messageList.reset();
    Alloy.Models.instance('userInfo').clear();
    Alloy.createController('login').getView().open();
    if(Ti.Platform.osname == 'iphone') {
        Alloy.Globals.projectsNavWin.close();
    } else if(Ti.Platform.osname == 'ipad') {
        Alloy.Globals.mainWinTablet.close();
    }
    Ti.App.Properties.removeProperty('userInfo');
    Ti.App.Properties.removeProperty('instanceInfo');
    logoutCloudUser();
}
I'm not sure if I need the conditions in front of the resets, but this is a result of much trial and error. I know that those reset statements work however. If I remove the controller parts of the function, calling logout() will clear the table that the collection is bound to.

No matter what I try, the collection models that should have been removed return and are added to the new models.

In the controller that gets the data from the server, I put in a reset at the top of the file after the collection definition, but that did nothing. This is the function that is run once when the controller loads and whenever the page is refreshed:

function refreshProjects(e) {
 
    Alloy.Globals.projectList.fetch();
 
    projectReq.open('POST', Alloy.Globals.getProjectsUrl);  
 
    projectReq.send({
        instance_id: userInfo.get('instance_id'),
        manager_id: userInfo.get('user_id')
    });
 
    Ti.UI.iPhone.setAppBadge(0);
 
}
What am I doing wrong?

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>