From f5bb57bbf4c93055d66beaff14a3b35dadabf60d Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Thu, 19 Oct 2017 22:04:21 -0700 Subject: [PATCH] Move the namespace query param up the route hierarchy This makes it active on all job routes --- ui/app/controllers/jobs.js | 28 ++++++++++++++++++++++++ ui/app/controllers/jobs/index.js | 37 +++++++++++--------------------- ui/app/routes/jobs.js | 22 ++++++++++++++++++- ui/app/routes/jobs/index.js | 17 +++++---------- 4 files changed, 66 insertions(+), 38 deletions(-) create mode 100644 ui/app/controllers/jobs.js diff --git a/ui/app/controllers/jobs.js b/ui/app/controllers/jobs.js new file mode 100644 index 000000000000..66b9dc9f9d3d --- /dev/null +++ b/ui/app/controllers/jobs.js @@ -0,0 +1,28 @@ +import Ember from 'ember'; + +const { Controller, inject, observer } = Ember; + +export default Controller.extend({ + system: inject.service(), + + queryParams: { + jobNamespace: 'namespace', + }, + + jobNamespace: 'default', + + // The namespace query param should act as an alias to the system active namespace. + // But query param defaults can't be CPs: https://github.com/emberjs/ember.js/issues/9819 + syncNamespaceService: observer('jobNamespace', function() { + const newNamespace = this.get('jobNamespace'); + const currentNamespace = this.get('system.activeNamespace.id'); + const bothAreDefault = + (currentNamespace == undefined || currentNamespace === 'default') && + (newNamespace == undefined || newNamespace === 'default'); + + if (currentNamespace !== newNamespace && !bothAreDefault) { + this.set('system.activeNamespace', newNamespace); + this.send('refreshRoute'); + } + }), +}); diff --git a/ui/app/controllers/jobs/index.js b/ui/app/controllers/jobs/index.js index 5e1c585feec8..3836b48c664f 100644 --- a/ui/app/controllers/jobs/index.js +++ b/ui/app/controllers/jobs/index.js @@ -2,7 +2,7 @@ import Ember from 'ember'; import Sortable from 'nomad-ui/mixins/sortable'; import Searchable from 'nomad-ui/mixins/searchable'; -const { Controller, computed, observer, inject } = Ember; +const { Controller, computed, inject } = Ember; export default Controller.extend(Sortable, Searchable, { system: inject.service(), @@ -16,25 +16,28 @@ export default Controller.extend(Sortable, Searchable, { searchTerm: 'search', sortProperty: 'sort', sortDescending: 'desc', - jobNamespace: 'namespace', }, currentPage: 1, pageSize: 10, - jobNamespace: 'default', sortProperty: 'modifyIndex', sortDescending: true, searchProps: computed(() => ['id', 'name']), - filteredJobs: computed('model.[]', 'jobNamespace', function() { - if (this.get('system.namespaces.length')) { - return this.get('model').filterBy('namespace.name', this.get('jobNamespace')); - } else { - return this.get('model'); + filteredJobs: computed( + 'model.[]', + 'system.activeNamespace', + 'system.namespaces.length', + function() { + if (this.get('system.namespaces.length')) { + return this.get('model').filterBy('namespace.id', this.get('system.activeNamespace.id')); + } else { + return this.get('model'); + } } - }), + ), listToSort: computed.alias('filteredJobs'), listToSearch: computed.alias('listSorted'), @@ -42,22 +45,6 @@ export default Controller.extend(Sortable, Searchable, { isShowingDeploymentDetails: false, - // The namespace query param should act as an alias to the system active namespace. - // But query param defaults can't be CPs: https://github.com/emberjs/ember.js/issues/9819 - syncNamespaceService: observer('jobNamespace', function() { - const newNamespace = this.get('jobNamespace'); - const currentNamespace = this.get('system.activeNamespace.id'); - const bothAreDefault = - currentNamespace == undefined || - (currentNamespace === 'default' && newNamespace == undefined) || - newNamespace === 'default'; - - if (currentNamespace !== newNamespace && !bothAreDefault) { - this.set('system.activeNamespace', newNamespace); - this.send('refreshRoute'); - } - }), - actions: { gotoJob(job) { this.transitionToRoute('jobs.job', job); diff --git a/ui/app/routes/jobs.js b/ui/app/routes/jobs.js index a5d83c8ac1d2..baa77cd81691 100644 --- a/ui/app/routes/jobs.js +++ b/ui/app/routes/jobs.js @@ -1,6 +1,6 @@ import Ember from 'ember'; -const { Route, inject } = Ember; +const { Route, inject, run } = Ember; export default Route.extend({ system: inject.service(), @@ -14,6 +14,26 @@ export default Route.extend({ return this.get('store').findAll('job'); }, + syncToController(controller) { + const namespace = this.get('system.activeNamespace.id'); + + // The run next is necessary to let the controller figure + // itself out before updating QPs. + // See: https://github.com/emberjs/ember.js/issues/5465 + run.next(() => { + if (namespace && namespace !== 'default') { + controller.set('jobNamespace', namespace); + } else { + controller.set('jobNamespace', 'default'); + } + }); + }, + + setupController(controller) { + this.syncToController(controller); + return this._super(...arguments); + }, + actions: { refreshRoute() { this.refresh(); diff --git a/ui/app/routes/jobs/index.js b/ui/app/routes/jobs/index.js index 2893e7e9ad0f..8962abd90006 100644 --- a/ui/app/routes/jobs/index.js +++ b/ui/app/routes/jobs/index.js @@ -1,18 +1,11 @@ import Ember from 'ember'; -const { Route, inject } = Ember; +const { Route } = Ember; export default Route.extend({ - system: inject.service(), - - setupController(controller) { - this._super(...arguments); - - const namespace = this.get('system.activeNamespace.id'); - if (namespace && namespace !== 'default') { - controller.set('jobNamespace', namespace); - } else { - controller.set('jobNamespace', 'default'); - } + actions: { + refreshRoute() { + return true; + }, }, });