This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
feature.js
84 lines (68 loc) · 2.15 KB
/
feature.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import Ember from 'ember';
const {
Service,
computed,
inject: {service},
Error: EmberError,
set
} = Ember;
export function feature(name) {
return computed(`config.${name}`, `labs.${name}`, {
get() {
if (this.get(`config.${name}`)) {
return this.get(`config.${name}`);
}
return this.get(`labs.${name}`) || false;
},
set(key, value) {
this.update(key, value);
return value;
}
});
}
export default Service.extend({
store: service(),
config: service(),
notifications: service(),
publicAPI: feature('publicAPI'),
subscribers: feature('subscribers'),
internalTags: feature('internalTags'),
_settings: null,
labs: computed('_settings.labs', function () {
let labs = this.get('_settings.labs');
try {
return JSON.parse(labs) || {};
} catch (e) {
return {};
}
}),
fetch() {
return this.get('store').queryRecord('setting', {type: 'blog,theme,private'}).then((settings) => {
this.set('_settings', settings);
return true;
});
},
update(key, value) {
let settings = this.get('_settings');
let labs = this.get('labs');
// set the new labs key value
set(labs, key, value);
// update the 'labs' key of the settings model
settings.set('labs', JSON.stringify(labs));
return settings.save().then(() => {
// return the labs key value that we get from the server
this.notifyPropertyChange('labs');
return this.get(`labs.${key}`);
}).catch((errors) => {
settings.rollbackAttributes();
this.notifyPropertyChange('labs');
// we'll always have an errors object unless we hit a
// validation error
if (!errors) {
throw new EmberError(`Validation of the feature service settings model failed when updating labs.`);
}
this.get('notifications').showErrors(errors);
return this.get(`labs.${key}`);
});
}
});