Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EmberJS: How to Redirect from Route, Keeping Query Params

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?

like image 288
user1429980 Avatar asked Jul 10 '15 17:07

user1429980


People also ask

Can you transition in Ember?

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.

Can we send query parameters in GET request?

Unless there's something sensitive in the request parameters, it is perfectly fine to send them as part of URL.

How do you query parameters in a 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.


1 Answers

You should be able to pass query parameters to the transitionTo, something along the line of:

this.transitionTo("applications", savedApplication, {
  queryParams: transition.queryParams
});
like image 184
Patsy Issa Avatar answered Oct 20 '22 06:10

Patsy Issa