Skip to content

Controller not reloaded when using back button #122

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
ffesseler opened this issue May 7, 2013 · 7 comments
Closed

Controller not reloaded when using back button #122

ffesseler opened this issue May 7, 2013 · 7 comments

Comments

@ffesseler
Copy link

I've some nested views.

When I go back using the browser back button from a child state to its parent state, the parent state controller is not reinstanciated.

Is it normal behaviour? Can I override it ?

@ksperling
Copy link
Contributor

As long as you remain within a parent state, and the parameters that belong to that parent don't change, the views and controllers don't get reloaded. This is a feature and is by design, otherwise every single view would be reloaded on every state transition.

If you want those views/controllers to be reloaded, declare them in the child state. But you should be able to write the controller so that it can handle child state changes without a reload.

@ffesseler
Copy link
Author

Hmm I see.

Let's say I've defined a parent/root state called : mystate
I'm using resolve in the state definition to get a list that will be displayed in a grid when I enter the state.

When I click on a grid element, I'm transitioning to a child state : mystate.detail
Now in that's state, I'm able to edit/remove the data and also transition to other child states (mystate.detail.otherstate)
Depending of what actions I will do in any of my child states, I will need to reload the list showed in the top level state (mystate) otherwise it won't be accurate.
Ideally I'd like to refresh that list only when I get back to the parent/root state.

Because there is no controller reload as long as I stay within a parent state, I'm not sure when/how to reload my list.
Should I listen to state transitions ?

@ksperling
Copy link
Contributor

Hm, maybe you can directly update the list (which presumably your controller in the parent state binds to the scope) from the child controller? Or the controller could expose a function on the scope to perform the reload, which you call from the child.

Listening for $stateChangeSuccess would also be an option -- as your controller only exists as long as it's state is active, you shouldn't receive any events for states that aren't children of the state that owns the controller (you might currently also receive the event for the entry and/or exit into the controllers own state -- the ui-view logic itself is triggered off $stateChangeSuccess at the moment).

@RamonDonnell
Copy link

I have a similar setup as @ffesseler. I have a list of items in the mystate, the child mystate.details is for editing/creating a single item. mystate uses resolve to get a list of items, but these are summary data, ie not the full items. mystate.details uses resolve to get the full item for editing.

So when saving/updating mystate.details sends the item the and the server and the app transitions back to mystate. So how to get the changed/new item into the mystate list! There are many ways to skin this cat, as already mentioned

  • mystate.details save service responds with the summary item and my state.details puts it back in the list that is available via parent scope
  • mystate.details on succussful save, then calls the same code as in the mystate resolve to refresh the list
  • manage the list via a service
  • use events to publish the new item...

Most of these seem to couple mystate.details to mystate, but I may want to reuse the mystate.details controller in another state so I rather it not need to know mystate. I'd rather not keep a reference of the list in a service and I generally keep event usage to a minimum. Also now mystate.details needs to deal with all the async responses to ensure the transition to mystate occurs after the item is in the list, but this is what resolve is supposed to solve.

Instead I'd prefer to be able to transition from my state.details to mystate but tell it to re-resolve, and re-create the controller only for mystate.

Any comments on if this, is it a feature request or something that makes sense for states and ui-router?

@ksperling
Copy link
Contributor

One option would be to make your 'details' child a sibling of the list state rather than a child, that would give you the reload behaviour you're after. However the ability to force-reload a state has been requested before -- I imagine it will be implemented at some point.

Personally I'd probably handle fetching the list and individual details and updating your client-side state in a custom service, rather than burdening the view controllers with all that stuff.

@ffesseler
Copy link
Author

I think the force-reload feature should be added in the roadmap then :)

@kompot
Copy link

kompot commented Sep 16, 2013

As it was mentioned above, one way (and seems most natural to me) is to refresh parent resolve value of a parent view in a child view.
I actually tried to find a way to accomplish that and could not find a way.

I'm sure there should be some kind of a one-liner to make this pseudo-code work.

$scope.childViewUpdate = function () {
  // do update
  $resolve.resolve($state.$current.parent.resolve)
}

Could please someone point me the way out?

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

No branches or pull requests

4 participants