github.com/outbrain/consul@v1.4.5/ui-v2/app/services/repository/dc.js (about) 1 import RepositoryService from 'consul-ui/services/repository'; 2 import { inject as service } from '@ember/service'; 3 import { get } from '@ember/object'; 4 import Error from '@ember/error'; 5 6 const modelName = 'dc'; 7 export default RepositoryService.extend({ 8 settings: service('settings'), 9 getModelName: function() { 10 return modelName; 11 }, 12 findAll: function() { 13 return get(this, 'store') 14 .findAll(this.getModelName()) 15 .then(function(items) { 16 return items.sortBy('Name'); 17 }); 18 }, 19 findBySlug: function(name, items) { 20 if (name != null) { 21 const item = items.findBy('Name', name); 22 if (item) { 23 return get(this, 'settings') 24 .persist({ dc: get(item, 'Name') }) 25 .then(function() { 26 // TODO: create a model 27 return { Name: get(item, 'Name') }; 28 }); 29 } 30 } 31 const e = new Error(); 32 e.status = '404'; 33 e.detail = 'Page not found'; 34 return Promise.reject({ errors: [e] }); 35 }, 36 getActive: function(name, items) { 37 const settings = get(this, 'settings'); 38 return Promise.all([name || settings.findBySlug('dc'), items || this.findAll()]).then( 39 ([name, items]) => { 40 return this.findBySlug(name, items).catch(function() { 41 const item = get(items, 'firstObject'); 42 settings.persist({ dc: get(item, 'Name') }); 43 return item; 44 }); 45 } 46 ); 47 }, 48 });