1
0
mirror of https://github.com/flarum/core.git synced 2025-07-21 08:41:17 +02:00

Allow custom variables to be set on the client app

This commit is contained in:
Toby Zerner
2015-07-31 20:08:27 +09:30
parent 1ccc9bee5f
commit c067db09d1
7 changed files with 92 additions and 39 deletions

View File

@@ -8,8 +8,8 @@ const app = new App();
app.initializers.add('store', store);
app.initializers.add('routes', routes);
app.initializers.add('preload', preload, -100);
app.initializers.add('preload', preload, -100);
app.initializers.add('boot', boot, -100);
export default app;

View File

@@ -2,6 +2,8 @@ import Component from 'flarum/Component';
import FieldSet from 'flarum/components/FieldSet';
import Select from 'flarum/components/Select';
import Button from 'flarum/components/Button';
import Alert from 'flarum/components/Alert';
import saveConfig from 'flarum/utils/saveConfig';
export default class BasicsPage extends Component {
constructor(...args) {
@@ -19,11 +21,11 @@ export default class BasicsPage extends Component {
];
this.values = {};
const config = app.forum.attribute('config');
const config = app.config;
this.fields.forEach(key => this.values[key] = m.prop(config[key]));
this.localeOptions = {};
const locales = app.forum.attribute('availableLocales');
const locales = app.locales;
for (const i in locales) {
this.localeOptions[i] = `${locales[i]} (${i})`;
}
@@ -113,7 +115,7 @@ export default class BasicsPage extends Component {
}
changed() {
const config = app.forum.attribute('config');
const config = app.config;
return this.fields.some(key => this.values[key]() !== config[key]);
}
@@ -124,14 +126,19 @@ export default class BasicsPage extends Component {
if (this.loading) return;
this.loading = true;
app.alerts.dismiss(this.successAlert);
const config = {};
this.fields.forEach(key => config[key] = this.values[key]());
app.forum.save({config}).finally(() => {
this.loading = false;
m.redraw();
});
saveConfig(config)
.then(() => {
app.alerts.show(this.successAlert = new Alert({type: 'success', children: 'Your changes were saved.'}));
})
.finally(() => {
this.loading = false;
m.redraw();
});
}
}

View File

@@ -0,0 +1,14 @@
export default function saveConfig(config) {
const oldConfig = JSON.parse(JSON.stringify(app.config));
Object.assign(app.config, config);
return app.request({
method: 'POST',
url: app.forum.attribute('adminUrl') + '/config',
data: {config}
}).catch(error => {
app.config = oldConfig;
throw error;
});
}