I want to redirect from a route, /new
, and keep the query params for the new
route:
As far as I know, the only place to access queryParams
is within the model
hook of a route.
But I want to redirect in the beforeModel
hook:
import Ember from "ember";
export default Ember.Route.extend({
/**
* @@override
* Implicitly create a new applicant, redirecting to the generated ID at /applications/#{id}
* @param transition
*/
beforeModel: function(transition) {
var emptyApplicant = this.store.createRecord("applicant",
{isPrimary: true}
),
emptyApplication = this.store.createRecord("application");
emptyApplication.set("applicant", emptyApplicant);
emptyApplicant.save().then((savedApplicant) => {
emptyApplication.save().then((savedApplication) => {
this.transitionTo("applications", savedApplication);
});
});
}
});
While the above code works, the transition will complete without preserving the query params. For example, navigating to applicants/new?agent=35
will not preserve agent=35
in the query param, and will simply redirect to applicants/new
instead.
How can I access the queryParams
object from the beforeModel
hook in my Ember app?
A Transition is a thennable (a promise-like object) that represents an attempt to transition to another route. It can be aborted, either explicitly via abort or by attempting another transition while a previous one is still underway. An aborted transition can also be retry() d later.
Unless there's something sensitive in the request parameters, it is perfectly fine to send them as part of URL.
Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.
You should be able to pass query parameters to the transitionTo
, something along the line of:
this.transitionTo("applications", savedApplication, {
queryParams: transition.queryParams
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With