Something went wrong on our end
Forked from
amiv / Admintool
226 commits behind the upstream repository.
-
Hermann authored
this comes together with a more unified layout of edit views the new structure simplifies data handling and needs less api communication, making it more responsive
Hermann authoredthis comes together with a more unified layout of edit views the new structure simplifies data handling and needs less api communication, making it more responsive
itemcontroller.js 1.38 KiB
import m from 'mithril';
import { ResourceHandler } from './auth';
export default class ItemController {
constructor(resource, embedded) {
this.resource = resource;
this.id = m.route.param('id');
if (this.id) {
this.modus = 'view';
} else {
this.modus = 'new';
this.data = {};
}
this.handler = new ResourceHandler(resource, false);
this.embedded = embedded || {};
if (this.id) {
this.handler.getItem(this.id, this.embedded).then((item) => {
this.data = item;
m.redraw();
});
}
}
post(data) {
return new Promise((resolve, reject) => {
this.handler.post(data).then((response) => {
this.id = response._id;
this.changeModus('view');
}).catch(reject);
});
}
patch(data, formData = false) {
return new Promise((resolve, reject) => {
this.handler.patch(data, formData).then(() => { this.changeModus('view'); }).catch(reject);
});
}
cancel() {
if (this.modus === 'edit') this.changeModus('view');
if (this.modus === 'new') m.route.set(`/${this.resource}`);
}
changeModus(newModus) {
this.modus = newModus;
if (this.modus === 'view') {
// reload item to current state, patches do not return embeddinds...
this.handler.getItem(this.id, this.embedded).then((item) => {
this.data = item;
m.redraw();
});
}
}
}