Emulating window.onbeforeunload for hash navigation

304 views
Skip to first unread message

Unirgy

unread,
Mar 7, 2011, 4:44:37 AM3/7/11
to Angular
Hi,

If I have a <ng:view/> template form open with unsaved changes, what
is the best way in angular to confirm with user navigation away from
the form?

Thanks,
Boris.

Igor Minar

unread,
Mar 7, 2011, 12:51:42 PM3/7/11
to ang...@googlegroups.com, Unirgy
There is no "direct" support for this.

Please file an RFE. In the meantime, you might (there might be some
complications) use $route.onChange to intercept hash changes.

/i

> --
> You received this message because you are subscribed to the Google Groups "Angular" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Unirgy

unread,
Apr 11, 2011, 1:01:11 AM4/11/11
to Angular
I've tried to submit issue on github, but getting error 500 :-\

Here it is:

$route.onChange() is too late, so I suggest adding $route.unless()
method with the following syntax:

// set verification for route change when first change made to the
form from controller:
$route.unless(this.myConfirmMethod);

// unset verification when form is saved or cancelled or navigation
away was confirmed:
$route.unless(null);

$route.unless() callback would be checked in the beginning of $route-
>updateRoute() method, and could contain something like this:

function myConfirmMethod() {
return confirm('You have unsaved changes, are you sure you
wish to navigate away?");

Igor Minar

unread,
Apr 11, 2011, 10:18:14 AM4/11/11
to ang...@googlegroups.com, Unirgy
do you need to intercept navigation only within your app or do you care about navigating outside of your app as well?

/i

Boris Gurvich

unread,
Apr 11, 2011, 1:28:45 PM4/11/11
to ang...@googlegroups.com
I need everything I can get :)

I'm also implementing lock/scheduled draft save functionality, so it will
give user max 30 seconds of unfinished work, in case they navigate outside
of the page.
But if I can have confirmation within the site, I'll take it.

Unirgy

unread,
Apr 11, 2011, 1:30:13 PM4/11/11
to ang...@googlegroups.com
And, navigating outside of the page can be taken care of regular onbeforeonload hook, right?

Igor Minar

unread,
Apr 11, 2011, 1:36:15 PM4/11/11
to ang...@googlegroups.com, Unirgy
yes

On Mon, Apr 11, 2011 at 10:30 AM, Unirgy <uni...@gmail.com> wrote:
And, navigating outside of the page can be taken care of regular onbeforeonload hook, right?

--

Igor Minar

unread,
Apr 11, 2011, 1:37:23 PM4/11/11
to ang...@googlegroups.com, Boris Gurvich
are you using links (<a href="..."></a>) for navigation within your app? can anything else cause the user to load a new route?

/i
Reply all
Reply to author
Forward
0 new messages