window.onbeforeunload removing observers?

65 views
Skip to first unread message

atlguy

unread,
Jan 24, 2012, 3:54:05 PM1/24/12
to Blast Mojo
I have build a controller that fires when a user makes changes to a
form but tries to leaves the page without saving changes. It's a
simple function:

window.onbeforeunload = function() {
return "You may lose some unsaved changes.";
};

What I'm finding is that all my mojo observers are lost and I have to
reinit mojo like I do in the sitemap.js to get them all back. So I
have 2 questions:

1. Is this a mojo specific issue?
2. Is there a better way to do this without mojo becoming unitialized?

I don't particularly want to reinit mojo because it fires off my
Omniture controller and is going to mess up my metrics.

Any suggestions?

Jaime Bueza

unread,
Jan 24, 2012, 4:00:33 PM1/24/12
to blast...@googlegroups.com
Unload event causes the JS VM to clean everything up. State is lost unless you're preserving it through local storage or cookies.

You may need to reinitialize all your controllers again by doing this: mojo.controller.Map.mapControllers() inside your onbeforeunload. If you're holding values on your form, you're probably safer by stringifying the serialized JSON representation of the form and then storing it into Local Storage.

window.onbeforeunload = function() {
       //throw confirmation dialog "You may lose some unsaved changes.";
       //reinitialize controllers by invoking mojo.controller.Map.mapControllers()
       //repopulate values from the form from localStorage
}; 

Hope that helps!
Jaime

Jaime Bueza

unread,
Jan 24, 2012, 4:05:47 PM1/24/12
to blast...@googlegroups.com
Also, just wondering, what version of Dojo are you running?

Cheers,
Jaime

Jaime Bueza

unread,
Jan 24, 2012, 9:31:02 PM1/24/12
to blast...@googlegroups.com
Hey Robert, 

Were you able to resolve this issue? Just checking up!

Cheers,
Jaime

atlguy

unread,
Jan 26, 2012, 10:02:40 AM1/26/12
to Blast Mojo
Hi Jaime, I added mojo.controller.Map.mapControllers(); and it seems
to work correctly. For some reason I tried that previously and it
didn't work so I obviously did something wrong. I did notice something
strange though. If I try to leave the form a 2nd time, none of the
observers seems to work any more even though I can still see them in
the DOM. Any idea what might cause that?

Jaime Bueza

unread,
Jan 26, 2012, 4:48:52 PM1/26/12
to blast...@googlegroups.com
It may be because Mojo bootstraps on dojo.init() -- it may not be invoking that on your 2nd time visiting the page. 

Does this behaviour affect all browsers? or just a specific subset of browsers?

Cheers,
Jaime

atlguy

unread,
Jan 27, 2012, 12:29:30 PM1/27/12
to Blast Mojo
I saw this in both IE and FF. I think I'm going to take a different
approach then window.onbeforeunload since it's causing too many
problems. Thanks again for your help!

Jaime Bueza

unread,
Jan 27, 2012, 12:43:06 PM1/27/12
to blast...@googlegroups.com, Blast Mojo
No problem. Feel free to use us as a sound board for any approaches you come up with!

Good luck!

Cheers
Jaime

Sent from my iPhone 4S

Reply all
Reply to author
Forward
0 new messages