How to monitor changes in query parameters in Ember?

How to observe several query parameters in Ember.js using query-params-new , without explicitly listing each of them 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 respects all these properties, but without having to list them, for example:

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

Edit:

I looped around the queryParams array and set the observer for each of them in the corresponding property:

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

He works as intended. Is there a better way to do this, or is it right?

+6
source share
1 answer

When the query parameter changes, the queryParamsDidChange action queryParamsDidChange triggered. This can be used in the controller or route to run any logic that needs to be run whenever the query parameter changes.

Example:

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

Example jsbin from PR

+16
source

All Articles