ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

ui-sref cause redundant state transition

Open buunguyen opened this issue 13 years ago • 37 comments

I noticed that whenever ui-sref is used, a state is transitioned to twice. Upon examining the source code, it looks like the issue is caused by the fact that $StateRefDirective not only generates a href but also binds onclick event. By removing one of those, the issue is solved.

Is this a bug or by design? Given that the built-in ng-href only generates href, is there a reason ui-sref has to bind the onclick event?

buunguyen avatar Aug 23 '13 20:08 buunguyen

If you look here, you'll notice that ui-sref suppresses the default click behavior, so for instances where you're clicking the link, the transition is handled by $state.go(), not by the link changing the URL.

nateabele avatar Aug 23 '13 20:08 nateabele

Hmm, you're right. Still, I have that issue and removing either the href or onclick stops it. Let me see if I can reproduce it in a plunker.

buunguyen avatar Aug 23 '13 20:08 buunguyen

AFAIK, you can't disable the browser's response to href in html.

laurelnaiad avatar Aug 23 '13 21:08 laurelnaiad

This plunker demonstrates the problem. Interestingly, the issue only happens when the function inside resolve runs asynchronously. I include both cases in that plunker, just comment out the first or second case and notice the difference when clicking the Next link.

buunguyen avatar Aug 23 '13 21:08 buunguyen

When I replace e.preventDefault() with return false, the problem disappears. But... only when jquery is referenced. If jquery is not used, the problem persists.

Examining jquery source, return false also causes e.stopPropagation() to be invoked. Adding e.stopPropagation() after e.preventDefault() fixes the issue regardless of whether jquery is used or not (see this plunker). But stopping propagation is obviously not a good idea.

I have no idea why propagation has a part in this (again, this issue occurs in both the simple plunker provided as well as my production app). Besides, back to the original question, why should ui-router generates href and onclick at the same time, why not just href like ng-href?

buunguyen avatar Aug 23 '13 22:08 buunguyen

Because states don't have to have URLs.

nateabele avatar Aug 23 '13 22:08 nateabele

Maybe the answer is to only do one or the other depending on if a URL is defined.

timkindberg avatar Aug 24 '13 00:08 timkindberg

Strongly disagree. One of the core concepts of this library is bridging the gap between new-school applications and standard Web conventions, which is what this enables.

nateabele avatar Aug 24 '13 00:08 nateabele

I'm not exactly sure why you think @timkindberg's suggestion defeats that purpose? I don't see how a user can possibly be affected by that change. (Doesn't mean I'm not interested in learning about the root cause & solving it.)

buunguyen avatar Aug 24 '13 01:08 buunguyen

Sorry, I misread it. I'll try and figure out a solution for this as soon as I can. Probably this weekend.

nateabele avatar Aug 26 '13 16:08 nateabele

I just submitted a PR, please take a look. (Sorry about multiple references in this thread; the PR has only 1 commit however.)

buunguyen avatar Aug 26 '13 18:08 buunguyen

Please see my comments on the PR. Unfortunately it doesn't really satisfy the requirements. I'll have to add some additional test coverage to uiSref so we don't have any regressions.

nateabele avatar Aug 26 '13 18:08 nateabele

@buunguyen Thanks for a quick-fix. I'll use yours until offical fix is applied. Since I have some ajax queries on my states' resolve, I don't want to fetch them twice. :)

ubenzer avatar Sep 11 '13 09:09 ubenzer

@ubenzer you're welcome. I'm also using the stopPropagation() hack in my app. Hope @nateabele have a chance to take a look at this soon.

buunguyen avatar Sep 11 '13 14:09 buunguyen

I also ran into this issue. I couldn't make the event.stopPropagation() hack work in my use case. Here's my workaround:

var off = scope.$on('$stateChangeStart', foo);

Calling off will remove the listener. So just call off in foo. Then it doesn't matter if the state change transition starts twice.

Obviously, there are lots of instances where this wouldn't work.

Looking forward to the real fix in ~ 0.4.0!

eupharis avatar Dec 31 '13 00:12 eupharis

Thanks @buunguyen - I just needed this line: if (isForm || nav) return;

Could we get this merged? Double triggering of state is a no-win.

foiseworth avatar Apr 10 '14 07:04 foiseworth

I ran into similar problem. A state transition is performed twice. It happens when my ui-sref doesn't include all of the state parameters. My intention is to have optional parameter. The first redirection supplies null as a value of the missing parameter and then the second redirections supplies "" in place of null. I know that ui-router doesn't really support optional parameters but this behaviour is odd. Maybe it will somewhat helpful for finding the cause

lubiluk avatar Apr 17 '14 14:04 lubiluk

I can confirm that the (as stated by @lubiluk) is with the ui-sref not including all of the state parameters. In my case I was trying to rely on parameters from parent routes already being resolved.

aramonc avatar May 06 '14 20:05 aramonc

+1 @lubiluk

The first redirection supplies null as a value of the missing parameter and then the second redirections supplies "" in place of null.

I am using the master branch which supposedly should support optional parameters. However, I get the same behavior with null being passed once, and "" the second time. I am working around this by passing in a "" instead of omitting the optional parameter and checking for "" in my controller.

charandas avatar Jun 11 '14 14:06 charandas

Still seeing an issue with the empty optional params that @lubiluk mentioned...would be nice to see a fix without a workaround. Not the end of the world as there is a workaround as @kbdaitch brings up, markup ends up looking like <li ui-sref="smart({id: ''})" >

afreeland avatar Aug 13 '14 14:08 afreeland