Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one observe changing query parameters in Ember?

Tags:

ember.js

How does one observe multiple query parameters in Ember.js, using query-params-new, without explicitly listing each one in observes(), or property().

Let's say I have the following list of query parameters:

queryParams: [
    'first',
    'second',
    'third',
    'fourth',
    'fifth',
    'sixth',
    'seventh',
    'eighth',
    'ninth',
    'tenth'
],

first: null,
second: null,
third: null,
fourth: null,
fifth: null,
sixth: null,
seventh: null,
eighth: null,
ninth: null,
tenth: null

I would like to have a method in the controller that observes all of these properties, but without having to list them, eg:

something: function() {
    ...
}.property('first', 'second', 'third', ...)

Edit:

I looped through the queryParams array, and set an observer for each of them, on the corresponding property, like so:

init: function () {
    this._super();
    var queryParams = this.get('queryParams');
    var self = this;
    queryParams.forEach(function (param) {
        self.addObserver(param, self, 'updateSelectedOptionsIfQueryParamsChange');
    });
},

It's working as intended. Is there a better way to do this, or is this correct?

like image 203
Nathan Lutterman Avatar asked Mar 15 '14 01:03

Nathan Lutterman


People also ask

How do you read a query parameter in Ember?

EmberJS - Query ParametersQuery parameters are specified on route driven controllers which appear to the right of the ? in a URL and are represented as optional key-value pairs. The above URL has the two query parameters; one is sort and the other is page which contains values ASC and 2 respectively.

How do you define query parameters?

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.

How do I find Route parameters and query strings?

import ActivatedRoute from '@angular/router'. Inject ActivatedRoute class in constructor. Access queryParams property of ActivatedRoute class which returns an observable of the query parameters that are available in the current URL route.


1 Answers

When a query param changes the action queryParamsDidChange is fired. This can be used in the controller or route to trigger what ever logic you need to run whenever a query param changes.

Example:

App.ApplicationRoute = Ember.Route.extend({
  actions: {
    queryParamsDidChange: function() {
      // do some funky stuff
    }
  }
});

Example jsbin from the PR

like image 125
manuel_mitasch Avatar answered Oct 19 '22 06:10

manuel_mitasch