Ghost/ghost/admin/app/services/settings.js
Kevin Ansfield cb59388c5b 💄🐷 sort-imports eslint rule (#712)
no issue

- adds `eslint-plugin-sort-imports-es6-autofix` dependency
  - implements ESLint's base `sort-imports` rule but has a distinction in that `import {foo} from 'bar';` is considered `multiple` rather than `single`
  - fixes ESLint's autofix behaviour so `eslint --fix` will actually fix the sort order
- updates all unordered import rules by using `eslint --fix`

With the increased number of `import` statements since Ember+ecosystem started moving towards es6 modules I've found it frustrating at times trying to search through randomly ordered import statements. Recently I've been sorting imports manually when I've added new code or touched old code so I thought I'd add an ESLint rule to codify it.
2017-05-29 20:50:03 +02:00

74 lines
2.1 KiB
JavaScript

import Ember from 'ember';
import RSVP from 'rsvp';
import Service from 'ember-service';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import get from 'ember-metal/get';
import injectService from 'ember-service/inject';
// ember-cli-shims doesn't export _ProxyMixin
const {_ProxyMixin} = Ember;
export default Service.extend(_ProxyMixin, ValidationEngine, {
store: injectService(),
// will be set to the single Settings model, it's a reference so any later
// changes to the settings object in the store will be reflected
content: null,
validationType: 'setting',
_loadingPromise: null,
// this is an odd case where we only want to react to changes that we get
// back from the API rather than local updates
settledIcon: '',
// the settings API endpoint is a little weird as it's singular and we have
// to pass in all types - if we ever fetch settings without all types then
// save we have problems with the missing settings being removed or reset
_loadSettings() {
if (!this._loadingPromise) {
this._loadingPromise = this.get('store')
.queryRecord('setting', {type: 'blog,theme,private'})
.then((settings) => {
this._loadingPromise = null;
return settings;
});
}
return this._loadingPromise;
},
fetch() {
if (!this.get('content')) {
return this.reload();
} else {
return RSVP.resolve(this);
}
},
reload() {
return this._loadSettings().then((settings) => {
this.set('content', settings);
this.set('settledIcon', get(settings, 'icon'));
return this;
});
},
save() {
let settings = this.get('content');
if (!settings) {
return false;
}
return settings.save().then((settings) => {
this.set('settledIcon', get(settings, 'icon'));
return settings;
});
},
rollbackAttributes() {
return this.get('content').rollbackAttributes();
}
});