github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/app/services/system.js (about)

     1  import Ember from 'ember';
     2  import PromiseObject from '../utils/classes/promise-object';
     3  import { namespace } from '../adapters/application';
     4  
     5  const { Service, computed, inject } = Ember;
     6  
     7  export default Service.extend({
     8    token: inject.service(),
     9    store: inject.service(),
    10  
    11    leader: computed(function() {
    12      const token = this.get('token');
    13  
    14      return PromiseObject.create({
    15        promise: token
    16          .authorizedRequest(`/${namespace}/status/leader`)
    17          .then(res => res.json())
    18          .then(rpcAddr => ({ rpcAddr }))
    19          .then(leader => {
    20            // Dirty self so leader can be used as a dependent key
    21            this.notifyPropertyChange('leader.rpcAddr');
    22            return leader;
    23          }),
    24      });
    25    }),
    26  
    27    namespaces: computed(function() {
    28      return this.get('store').findAll('namespace');
    29    }),
    30  
    31    shouldShowNamespaces: computed('namespaces.[]', function() {
    32      const namespaces = this.get('namespaces').toArray();
    33      return namespaces.length && namespaces.some(namespace => namespace.get('id') !== 'default');
    34    }),
    35  
    36    activeNamespace: computed('namespaces.[]', {
    37      get() {
    38        const namespaceId = window.localStorage.nomadActiveNamespace || 'default';
    39        return this.get('namespaces').findBy('id', namespaceId);
    40      },
    41      set(key, value) {
    42        if (value == null) {
    43          window.localStorage.removeItem('nomadActiveNamespace');
    44        } else if (typeof value === 'string') {
    45          window.localStorage.nomadActiveNamespace = value;
    46          return this.get('namespaces').findBy('id', value);
    47        }
    48        window.localStorage.nomadActiveNamespace = value.get('name');
    49        return value;
    50      },
    51    }),
    52  });