I have updated the code to use $cookieStore in the 'beforeunload' handler and make a call to $scope.$apply() which ensures the document.cookie object is updated with the $cookieStore content before the handler returns.
'http://plnkr.co/edit/9a4e9P?p=preview' Does this approach raise any alarms with any angular experts out there? $apply is a synchronous call right? On Friday, January 24, 2014 1:05:29 PM UTC, Gary Cuthbert wrote: > > I have made some progress with this and have updated the plunk to > demonstrate: > > 'http://plnkr.co/edit/9a4e9P?p=preview' > > Below is my attempt to explain the behavior which sounds plausible to me. > I do also have a question about triggering an angular update right at the > end of this post, perhaps someone could advise if this is possible in this > context and if so how to do it?? > > Many thanks > -- > > As i suspected it appears to be a timing issue as to when the $cookies > object is updates/updates the main document.cookie. > > I have added a method to my controller 'updateDocumentCookie' which > updates the $window.document.cookie object directly. If i call this method > from my 'unloadProcessing()' handler then the updates are applied to the > document.cookie object and can be found within the angular $cookies object > when the controller 'startup()' method is called following a refresh. > > Conversely during 'startup()' i make a change to the $cookie object and > check the document.cookie object, i can confirm at this point the change > has not propagated to the document.cookie. On a subsequent call to > unloadProcessing (by triggering a refresh) it can be seen that the > controller changes made to $cookies are propogated to the document.cookie > object by the time this handler is called. > > To reproduce this with the plunk click on the link and run it. Clear you > browser cache, open your browser debug output console and clear it, hit the > 'trigger a manual refresh of the preview'. You should get something like > the following in the browser debug console: > > unloadProcessing() called $cookies: > Object {} > document.cookies : > *As expected following a cache clear both $cookies and document.cookies > are empty* > > unloadProcessing() returning $cookies: > Object {} > document.cookies : > ____cookieVal1=%22set in unloadProcessing!%22 > *Having manipulated document.cookies in the 'unloadProcessing' handler we > can see that the document.cookies object has been updated but the $cookies > object has not.* > > *Now on reload the startup() method on AppController is ultimately > called...* > startup() called $cookies: > Object {____cookieVal1: ""set in unloadProcessing!""} > document.cookies : > ____cookieVal1=%22set in unloadProcessing!%22 > *On the initial load the $cookies object reflects the doocument.cookie > object* > > Applied change to $cookieStore for "____cookieVal2" during startup. > $cookies: > Object {____cookieVal1: ""set in unloadProcessing!"", ____cookieVal2: ""value > 2 set in startup!""} > document.cookies : > ____cookieVal1=%22set in unloadProcessing!%22 > > *The startup() method changes the content of $cookies by calling the put > method on $cookieStore, the changes to $cookie can be seen and it can also > be seen that the document.cookies object has not yet been updated. If you > hit refresh again you will see the $cookie content has update the > document.cookie object by the time unloadProcessing is called.* > > *---* > > *Q. As you may expect if the document.cookie object is updated in > startup() instead of $cookieStore it does not apply the change to $cookie. > Presumably this is because angular needs to be triggered to update which > makes me wonder if it is viable to manipulate the $cookieStore object in > the unloadProcessing() handler and trigger an angular update to flush the > changes to document.cookie before the handler call returns?* > > 1. > > > On Thursday, January 23, 2014 6:15:14 PM UTC, Gary Cuthbert wrote: >> >> I have put together a plunk to demonstrate, i would really appreciate the >> benefit of your expertise... >> >> 'http://plnkr.co/edit/9a4e9P?p=preview' >> >> If you run in the plunker preview window, clear your cookie cache and hit >> the 'manually trigger a refresh of the preview' button you will get >> something like the following in the console window: >> >> unloadProcessing() called $cookies: >> Object {____cookieVal2: ""value 2 set in startup!""} >> unloadProcessing() returning $cookies: >> Object {____cookieVal2: ""value 2 set in startup!"", ____cookieVal1: ""set >> in unloadProcessing!""} >> startup() called $cookies: >> Object {____cookieVal2: ""value 2 set in startup!""} >> startup() returning $cookies: >> Object {____cookieVal2: ""value 2 set in startup!""} >> >> Is it too late by the time the 'unloadProcessing' method is called to >> make changes to $cookieStore? >> >> Many thanks >> >> On Wednesday, January 22, 2014 5:01:20 PM UTC, Gary Cuthbert wrote: >>> >>> Hello, >>> >>> I am currently investigating the use of $cookieStore to record some >>> trivial application settings and would like to update the $cookieStore when >>> the application is going through the process of tear down (either when the >>> user hits the browser reload button or closes the window or navigates away >>> from the application). >>> >>> Is this possible? I have a controller associated with my main <div >>> ng-app="app"> element and I have tried trapping for $destroy in this >>> controller and attempting the updates in its handler but the event is not >>> trapped. >>> >>> I could update the $cookieStore periodically in this case but i suspect >>> sooner or later i will need to be able to perform application cleanup logic >>> so in general how would/can you detect that an angular application is about >>> to close? >>> >>> Many thanks >>> >> -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/groups/opt_out.
