mercredi 25 mars 2015

Angular, location change event to fire only when the user modifies or gets sent a new url


So i have a little custom "state" library, and I am trying to determine between 2 different events to make it easier on myself. I am storing state in a url string and have set up a module to handle all the events around dealing with it.


So I let users chnage state from their modules by calling



myModule.goState(new state here);


And this will change the url string and all the required events around it.


Now the problem I am having is hooking onto an event where that does no change via the "gostate". So what I mean is, sometimes the user will be linked to a page where it needs to read the parameters out of the url, or maybe the user modifies the url by hand to change something. Either way I'm handling it right now by using a listener to like so:



$rootScope.$on('$locationChangeSuccess', function(event) {

//detect changes and change state accordingly


So - this works great, however the issue with this method is it fires when I use a goState call in a controller, because it does change the url string with a $locaiton.search(). And what I would love to be able to have is some way of not having that happen.


So when I fire gostate - this listener would not react to it. I know locationChangeSuccess will cause trouble for this, because when I call $location.search(mynewstateurl) it changes the string and that listener would fire.


So I'm wondering if there would be some kind of way to separate these 2 types of events. Hopefully I did an alright job of trying to explain this! Thanks for reading, looking forward to any ideas!


Can see full idea here - http://ift.tt/1NalmlT


if you check the logs, you can see "change" is firing when you click the buttons (that fire gostate)


TLDR : how do you differentiate the locationChangeSuccess (or other event?) from when it's set from a $location.search() to when it's manually changed (user types in url) or when the user lands on a new page.





Aucun commentaire:

Enregistrer un commentaire