3

I'm writing an action handler in route:application:

actions: {
  changeFoo(foo) {
    // I want to change the fooId queryParam to foo.get('id')
  }
}

The problem is that the only documented ways I can find to change the query params are transitionTo('some.route', someModel, { queryParams: { ... } } and the replaceWith version of the same. But I'm in route:application, so I don't know the current route's name. That means I don't know what the first argument to transitionTo would be.

Is there another way to get the URL to become ?fooId=123?

James A. Rosen
  • 64,193
  • 61
  • 179
  • 261

2 Answers2

10

You don't need current route name. You can just do 'transitionTo({queryParams: { foo: 123 })'. The router will apply it to the correct route.

  • Yep, you're right: this works. It's not documented, but used internally by Ember. – James A. Rosen Feb 04 '16 at 04:36
  • 4
    It's documented in the [guides](https://guides.emberjs.com/v2.3.0/routing/query-params/#toc_transitionto) and [api docs](http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo) – dwickern Feb 04 '16 at 05:14
1

From controller:application you can set the query param foo like this:

import Ember from 'ember';

export default Ember.Controller.extend({
  queryParams: ['foo'],

  actions: {
    updateMyQueryParam() {
      this.set('foo', 'hello');
    }
  }
});
mschoening
  • 93
  • 1
  • 5
  • I tried doing the following from my route action, but it didn't take: `this.controllerFor('application').set('foo', 123);` – James A. Rosen Feb 04 '16 at 04:29
  • Here is the same code with the action on the route: https://github.com/max/ember-query-params/commit/afbe8e9091be879d53f3b54dcca6735ba13d80ac What version of Ember are you running? – mschoening Feb 04 '16 at 04:35