Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($rootScope): remove history event handler when app is torn down #9905

Closed
wants to merge 1 commit into from

Conversation

randombk
Copy link
Contributor

@randombk randombk commented Nov 4, 2014

Remember the popstate and hashchange handler registered with window
when the application bootstraps, and remove it when the application
is torn down

Closes #9897

@mary-poppins
Copy link

I'm sorry, but I wasn't able to verify your Contributor License Agreement (CLA) signature. CLA signature is required for any code contributions to AngularJS.

Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match.

If you signed the CLA as a corporation, please let us know the company's name.

Thanks a bunch!

PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR.
PS2: If you are a Googler, please sign the CLA as well to simplify the CLA verification process.

@randombk
Copy link
Contributor Author

randombk commented Nov 4, 2014

Fixed.

* Note: this method is used only by $rootScope to remove the handler when an app
* is torn down. (See https://github.com/angular/angular.js/issues/9897)
*/
self.cacheStateAndFireUrlChange = function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

@lgalfaso
Copy link
Contributor

lgalfaso commented Nov 4, 2014

this is somehow a duplicate of #8005

@randombk
Copy link
Contributor Author

randombk commented Nov 4, 2014

You're right - the $shutdown service looks like a cleaner way to do this. Why was work on it stopped?

@caitp
Copy link
Contributor

caitp commented Nov 4, 2014

it's not really much cleaner, it's much harder to land something like $shutdown because it's way more complicated.

This patch is pretty simple, it's a good way to go.

@@ -294,6 +294,19 @@ function Browser(window, document, $log, $sniffer) {
};

/**
* @name $browser#$$applicationDestroyed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private API, don't use doc annotations for this --- just a comment explaining what it does

@lgalfaso
Copy link
Contributor

lgalfaso commented Nov 4, 2014

I agree with @caitp that the shutdown idea is more complex than this patch (with all that this implies). The only thing in favor is that it takes care of cleaning up $timeout, $interval and provides a mechanism for third party modules to perform some tasks when shutting down an angular app

@randombk
Copy link
Contributor Author

randombk commented Nov 4, 2014

@lgalfaso I guess modules can just listen to the $destroy event on $rootScope for that

@randombk
Copy link
Contributor Author

randombk commented Nov 7, 2014

Is there still an issue with the CLA? I notice that this is still tagged cla:no

@caitp
Copy link
Contributor

caitp commented Nov 7, 2014

@randombk the robot is having some issues with auto CLA updates lately :( I thought it was supposed to be resolved by now, but I guess not...

@caitp
Copy link
Contributor

caitp commented Nov 7, 2014

I don't think we're going to get this in for this release, but it seems like a reasonable approach to me, so we can probably do this next time.

I'm half expecting to hear complaints from people that their app no longer "works" because of this, though :(

* NOTE: this api is intended for use only by $rootScope.
*/
self.$$applicationDestroyed = function() {
jqLite(window).unbind('popstate', cacheStateAndFireUrlChange);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

off should probably be used instead of the legacy unbind. Could also make it one line doing jqLite(window).off('popstate hashchange', cacheStateAndFireUrlChange);

Remember the popstate and hashchange handler registered with window
when the application bootstraps, and remove it when the application
is torn down

Closes angular#9897
@googlebot
Copy link

CLAs look good, thanks!

@googlebot googlebot removed the cla: no label Dec 3, 2014
@petebacondarwin
Copy link
Member

I would rather see this event removal closer to where it is added, in $browser. Can't we put it in a $rootScope.$on('$destroy', function() { ... }) instead, which could be created in the $browser.onUrlChange() method?

@Narretz
Copy link
Contributor

Narretz commented Mar 12, 2015

@petebacondarwin I can open a new PR with this change. Depends on #11241 though.

@randombk
Copy link
Contributor Author

@petebacondarwin is $rootScope available from $browser.onUrlChange()? $browser is injected into $rootScope.

@petebacondarwin
Copy link
Member

How about putting the $rootScope.$on('$destroy', function() { ... }) inside the $location, which is where $browser.onUrlChange() is called?

For example, if onUrlChange returned a method that could be called to unbind itself then we could easily do that with little coupling there.

@randombk
Copy link
Contributor Author

I can move the removal method call to where you suggest, but I think we should still keep $browser.$$applicationDestroyed(). $browser.onUrlChange() currently returns the registered handler, which makes sense. I know this is an internal api, but making it return a function that removes a different handler is fairly non-intuitive.

@petebacondarwin
Copy link
Member

Added some tests and merged. Thanks

netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
Remember the popstate and hashchange handler registered with window
when the application bootstraps, and remove it when the application
is torn down

Closes angular#9897
Closes angular#9905
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove popstate/hashchange event handler when app is torn down
8 participants