First of all, I’ve asked the question at the SO at Sep 2. It didn’t get enough attention.
While going from one route to another, I want to pass some data (especcially arrays). How is it possible?
I’ve found some solutions with drawbacks:
by using query-params: need to serialize, deserialize. Seems not suitable for different-sized arrays.
by using a service to store data: it is not only available to routes, but available to whole app. It can be modified unintentially. Also it will not have a garbage remove mechanism. It stores the values from routes that was displayed far before.
by using transition object as promise: It modifies the returning hash of model hook. Involves in someone else’s business.
by using transition object’s data attribute: It fits well. The only problem we found is: it is not working on page refreshs. (Of course page refresh means to reload the whole SPA. But it is totally acceptable from the users’ perspective. Also using services may cause this problem.)
So, what is the suitable approach to passing parameters between different routes?
I believe service is all you need. If you need to invalidate some resources you should do it manually.
Could you provide some more context please? Do you need to share data between nested routes? Do you need to share the same resource by id? How does your router.js look like?
After all, I found that query-params is well suitable.
But, I need to override serializeQueryParam and deserializeQueryParam hooks to send complex objects.
No matter in arrays, if I define them as the following in my route:
queryParams: {
arr:{
type:'array'
}
},
As a workaround, I can send all complex objects in an array as following:
Further more about query-params: It has a feature “stickyness”. But actually it is not a feature, but a bug!
According to the guide:
If you wish to reset a query param, you have two options:
explicitly pass in the default value for that query param into link-to or transitionTo.
use the Route.resetController hook to set query param values back to their defaults before exiting the route or changing the route’s model.
According to option 1:
If you want add some queryParams to one of your route, you need to change all your current code that navigates to that route to reset your new query parameters. If you don’t do that, your route will open with your last query-params!
According to option 2:
Actually, option 2 is not working in case of the current route is not changing. For example: You have a menu that is rendered by application.hbs and if user selects to go to the X route while at X route (with some query params), resetController hook will not work. And current query params are still be displayed. I saw some addons relying on this hook. But they cannot solve the problem of staying on the same route.
Anyway, if ones wrote an addon that tries to eliminate one of behaviours of the framework, isn’t it an indicator of that behaviour should be closed with a config parameter?
EDIT:
Third option is to override one of the private methods of Router: see working in twiddle. Method is: _hydrateUnsuppliedQueryParams
Assuming you have a products page where you can see 10 different products and you want to open multiple tabs together(new route). What method do you think fits in this use case? I need to keep the selected product data so a page refresh wouldn’t lose it. Would query-params be the right choice?
I know this sounds kind of messy, but why not just add the complex Object/Array that you need in the next route directly into the new route’s model hash? Assuming of course your new route is returning an RSVP.hash from its model (easy refactor if it isn’t):
It’s not a beautiful solution; but it is something you’re free to do during transitions. This will not work if the transition is opened in a new tab and it won’t do anything for page refreshes. I hope that’s obvious.
This is very convenient in passing complex information from route to route; usually improving your end-user’s experience.
Route intercourse isn’t pretty but it’s a means to an end