PDA

View Full Version : Safely clearing out the DOM



bdvr
28 Jul 2011, 1:37 PM
What is the best way to go about cleaning out the DOM all at once, as opposed to calling the destroy method on components along the way? For instance, in my app, when a user logs out, the app navigates back to a login screen. Before showing the login screen again, I'd like to clean out the DOM (and preferably and the listeners too), and have the app be pretty much like it was when it first started--essentially reseting the app.

I have tried viewport.destroy() and viewport.removeAll(true), and while they do a good job of cleaning the DOM, they cause errors when rendering my login screen views. Is there a better way?

I could just do window.location.reload(), but that's a hack. My app has a splash screen as a background image, which then gets replaced with the login view once ST finishes loading, so it's disorienting to see the splash screen, then the login screen instead of just going straight to the login screen after clicking the logout button.

Thoughts anyone?

jjerome
29 Jul 2011, 4:51 AM
All I can think of is manually adding each component to an array as it is added/created.

Then when you choose to log out, use a method(function) to go through the array and call a destroy() on each item in that array.

mschwartz
29 Jul 2011, 5:22 AM
Set a cookie and reload. If the cookie is set, don't show the splash screen. Clear the cookie upon login.

You probably do want to reload the app from scratch.

bdvr
29 Jul 2011, 5:39 AM
I like that approach, mschwartz. Instead of the cookie, though, I think I'll use the MVC URL routing mechanism.

I have a URL route called #Main that runs when the app starts normally. For the logout, I'll set window.location.href to another route called #Login. #Login will go directly to the login controller action and bypass other startup logic I have that takes some time.

Then when the app loads, instead of checking for the cookie, I'll check window.location.hash, and if it's not #Main, then I'll hide the splash screen.

Thanks a lot!

Thanks you for your suggestion as well, jjerome. That would probably work, although it's probably more toil than I was looking to do. ;)