Skip to main content
March 24, 2010
Question

Resetting the ModelLocator

  • March 24, 2010
  • 1 reply
  • 611 views

The application that I am currently working on requires the ability for a user to log out of the application.  Logging out should effectively reset the entire state of the application back to what it was when the user first showed up to the login page.  The best solution would be to to just refresh the browser, but that is not an option.  I need the ability to reset the ModelLocator so that all of the views that bind to it revert back to the login state.  I can create a reset() function on ModelLocator and manually reinstantiate each property, but I'm looking for a better solution.  Since ModelLocator is a singleton, the static constant "_instance" (along with the "locked" constructor - forcing the user to use getInstance to get a reference) can only refer to one instance of ModelLocator.  I first tried a public reset function that would call the constructor and pass in the reference to the "key" (private function key), which I was hoping would reinstantiate the static constant "_instance" and therefore force all the views to rebind.  However, "_instance" does have a reference to a fresh instance, but any views that have stored a reference to ModelLocator ([Bindable]private var model:ModelLocator = ModelLocator.getInstance();) will NOT get these bindings triggered.  This is because that "private var model" is still holding a reference to the FIRST ModelLocator instance that was created.  Is there a way to accomplish what I am after, or am I forced to manually reset each property on ModelLocator?

This topic has been closed for replies.

1 reply

Participating Frequently
March 24, 2010

I think you should reset each property. But if you have many different properties on your ModelLocator so that this becomes cumbersome, that suggest an opportunity to refactor your ModelLocator. Create dedicated objects with fewer responsilities that you might only instantiate on the ModelLocator. (or an IoC container if you're also thinking about using one).

Also note that reseting your Flex app without a brower refresh can be an effort if you're not instantiating your views. Then, if you have not extracted all view state into objects that you reset (via i.e. your ModelLocator), you could run into problems. Why isn't a browser refresh possible?