2
0
mirror of https://github.com/frappe/books.git synced 2024-11-13 00:46:28 +00:00
books/client/desk/formpage.js
2018-02-06 22:45:17 +05:30

43 lines
1.1 KiB
JavaScript

const Page = require('frappejs/client/view/page');
const view = require('frappejs/client/view');
const frappejs = require('frappejs');
module.exports = class FormPage extends Page {
constructor(doctype) {
let meta = frappe.get_meta(doctype)
super(`Edit ${meta.name}`);
this.meta = meta;
this.form = new (view.get_form_class(doctype))({
doctype: doctype,
parent: this.body
});
this.on('show', async (params) => {
await this.show_doc(params.doctype, params.name);
});
// if name is different after saving, change the route
this.form.on('submit', async (params) => {
let route = frappe.router.get_route();
if (this.form.doc.name && !(route && route[2] === this.form.doc.name)) {
await frappe.router.set_route('edit', this.form.doc.doctype, this.form.doc.name);
this.form.show_alert('Added', 'success');
}
});
this.form.on('delete', async (params) => {
await frappe.router.set_route('list', this.form.doctype);
});
}
async show_doc(doctype, name) {
try {
this.doc = await frappe.get_doc(doctype, name);
this.form.use(this.doc);
} catch (e) {
this.render_error(e.status_code, e.message);
}
}
}