Skip to content

state transition performed twice when using optional state parameters #1396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3plusalpha opened this issue Sep 23, 2014 · 24 comments
Closed
Assignees
Labels
Milestone

Comments

@3plusalpha
Copy link

When optional state parameters are defined for a state, the state transition is performed twice as long as the optional parameters.

The $stateParams of a controller that was called twice look like this:
First: {offset: undefined, limit: undefined, sort: undefined, sortby: undefined}
Second: {offset: 0, limit: 20, sort: "asc", sortby: "title"}

Note that all state parameters are optional and defined like this.

.state({
  name: "list.products",
  parent: "list",
  url: "/products/:offset/:limit/:sortby/:sort/",
  params: {
    offset: { value: 0 },
    limit: { value: 20 },
    sort: { value: 'asc' },
    sortby: { value: 'title' }
  },
  templateUrl: "views/partials/product-search-result-item.html",
  controller: "ListProductsController"
})

When I use ui-sref without any parameter the controller is called twice.
Products

Using all state parameters the controller is called only once.
Products

IMHO this is a bug and I hope somebody can fix it as all controllers with optional parameters and API calls make API requests twice.

@christopherthielen
Copy link
Contributor

I think I've seen this as well. Will you please make a plunk?

@3plusalpha
Copy link
Author

Here is the plunkr:
http://plnkr.co/edit/uQEznMA2fadSYQg3fCvc

As soon as I added the ID parameter to the URI (route1/:id) the controller is called twice

@christopherthielen
Copy link
Contributor

Here's what's happening:

  1. ui-sref calls transitionTo
  2. transitionTo was not applying default params when building toParams.
  3. transitionTo completes and saves the partial toParams as fromParams
  4. after the transition, the URL is sync'd.
  5. the sync code uses UrlMatcher which applies default params, and calls transitionTo
  6. normally, transitionTo exits early since the state and params are identical. However, in this case, the now fromParams (step 3's toParams) don't have defaults, while step 4's params have them, so the transition occurrs again.

patched plunk with fix from #1424
http://plnkr.co/edit/Wiej3lypbhPKFkAsRNoZ?p=preview

@3plusalpha
Copy link
Author

Thank you Chris for digging into that issue!

@uptownhr
Copy link

uptownhr commented Nov 2, 2014

hi, i'm also experiencing this using routeProvider, '/page/:id?' has this fix also addressed routeProvider as well?

@nolandubeau
Copy link

Hi, I'm seeing this when using version 0.2.13. Instead of using ui-sref I have am using ng-click to call a function which then calls $state.go(). I can also confirm this when using $state.transitionTo().

ng-click="selectAlert(alert)

var selectAlert = function(alert){
$state.go('app.alerts.list.detail', {alertId: alert.id});
}

Does this bug still exist or am I using this incorrectly?

@realdah
Copy link

realdah commented May 2, 2015

I had same problem like @nolandubeau . version is 0.2.11.
call $state.go in ng-click. some $state parameters are null.

Seems to be fixed in 0.2.14.

@pranaydutta89
Copy link

i am still getting the same problem in 0.2.15 ,is the problem fixed or still persist ,please help me with this one

@bryanerayner
Copy link

I also am getting this problem in 0.2.15. I am using custom types in my solution.

@christopherthielen
Copy link
Contributor

Please make a plunker

@Gratouille
Copy link

+1, have the same problem in 0.2.15.

@pranaydutta89
Copy link

its working fine with 0.2.14,

@Gratouille
Copy link

Ok, nevermind, it was a problem of implementation of my login router. In fact I did it in my $stateChangeStart and it didn't end well. So I think I have to move this out in another listener.

@blowsie
Copy link

blowsie commented Sep 16, 2015

I agree with @pranaydutta89; for me using version 0.2.14 instead if 0.2.15 fixes my issue.
There does seem to be another issue here, which I am looking into.

@bryanerayner
Copy link

@christopherthielen I have a Plunkr which demonstrates what I'm trying to do to make some sort of dynamic parameters work in 0.2.15. http://plnkr.co/edit/Yeex1Rmd1nWQvdrxj87N?p=preview. It works to change parameters syncronously, but not via a $timeout.

@levivm
Copy link

levivm commented Oct 8, 2015

Im getting this problem in 0.2.15.

Using 0.2.14. fixed the problem to me.

@m2omou
Copy link

m2omou commented Oct 19, 2015

+1 Same for me, I had to go back to 0.2.14 to fix the problem

@petr0001
Copy link

+1 Same here

@m2omou
Copy link

m2omou commented Oct 20, 2015

Actually it's also happening in the version 0.2.14 even though this version looks more stable than the 0.2.15.

@zszep
Copy link

zszep commented Oct 22, 2015

Can confirm this for version 0.2.15. When using nested views and a parameter is present in ui-sref, then the parent resolve function is fired for a second time. Removing the parameter makes it work as expected.

@vroudge
Copy link

vroudge commented Feb 4, 2016

Can also confirm on 0.2.15

@christopherthielen
Copy link
Contributor

@bryanerayner Thanks for the plunker, but this ticket is about "state transition performed twice when using optional state parameters". Your plunker is demonstrating some other issue with reloadOnSearch (we only seem to support one level of reloadOnSearch at a time). Dynamic parameters are scheduled for the 1.0 release.

Also, you should use inherit: true so you don't lose your current parameters.
http://plnkr.co/edit/hMRjZSnLrjnCKnI5eqeT?p=preview

@christopherthielen
Copy link
Contributor

To the rest of you who have +1'd or said "me too", I need you to reproduce this double-transition issue in a plunker. This ticket is closed, the original issue was resolved, and I don't have any usable information to go on to fix the +1s

@yamateh
Copy link

yamateh commented Oct 18, 2016

Im getting this problem in 0.2.15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests