2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 20:11:15 +00:00

Merge pull request #156 from 18alantom/shift-init

refactor: move fjs init boilerplate to fjs
This commit is contained in:
Alan 2021-11-29 15:16:49 +05:30 committed by GitHub
commit 650b9f6191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,16 @@ const Observable = require('./utils/observable');
const utils = require('./utils'); const utils = require('./utils');
module.exports = { module.exports = {
async init() { initializeAndRegister(customModels = {}) {
this.init();
const common = require('frappejs/common');
this.registerLibs(common);
const coreModels = require('frappejs/models');
this.registerModels(coreModels);
this.registerModels(customModels);
},
init() {
if (this._initialized) return; if (this._initialized) return;
this.initConfig(); this.initConfig();
this.initGlobals(); this.initGlobals();
@ -15,7 +24,7 @@ module.exports = {
this.config = { this.config = {
serverURL: '', serverURL: '',
backend: 'sqlite', backend: 'sqlite',
port: 8000 port: 8000,
}; };
}, },
@ -47,7 +56,9 @@ module.exports = {
`Model name mismatch for ${doctype}: ${metaDefinition.name}` `Model name mismatch for ${doctype}: ${metaDefinition.name}`
); );
} }
let fieldnames = (metaDefinition.fields || []).map(df => df.fieldname).sort(); let fieldnames = (metaDefinition.fields || [])
.map((df) => df.fieldname)
.sort();
let duplicateFieldnames = utils.getDuplicates(fieldnames); let duplicateFieldnames = utils.getDuplicates(fieldnames);
if (duplicateFieldnames.length > 0) { if (duplicateFieldnames.length > 0) {
throw new Error( throw new Error(
@ -103,9 +114,9 @@ module.exports = {
method: 'POST', method: 'POST',
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json' 'Content-Type': 'application/json',
}, },
body: JSON.stringify(args || {}) body: JSON.stringify(args || {}),
}); });
return await response.json(); return await response.json();
}, },
@ -126,7 +137,7 @@ module.exports = {
} }
// propogate change to `docs` // propogate change to `docs`
doc.on('change', params => { doc.on('change', (params) => {
this.docs.trigger('change', params); this.docs.trigger('change', params);
}); });
} }
@ -174,7 +185,7 @@ module.exports = {
if (!doc) { if (!doc) {
doc = new (this.getDocumentClass(doctype))({ doc = new (this.getDocumentClass(doctype))({
doctype: doctype, doctype: doctype,
name: name name: name,
}); });
await doc.load(); await doc.load();
this.addToCache(doc); this.addToCache(doc);
@ -196,7 +207,7 @@ module.exports = {
for (let field of this.getMeta(doc.doctype).getValidFields()) { for (let field of this.getMeta(doc.doctype).getValidFields()) {
if (['name', 'submitted'].includes(field.fieldname)) continue; if (['name', 'submitted'].includes(field.fieldname)) continue;
if (field.fieldtype === 'Table') { if (field.fieldtype === 'Table') {
newDoc[field.fieldname] = (doc[field.fieldname] || []).map(d => { newDoc[field.fieldname] = (doc[field.fieldname] || []).map((d) => {
let newd = Object.assign({}, d); let newd = Object.assign({}, d);
newd.name = ''; newd.name = '';
return newd; return newd;
@ -255,7 +266,7 @@ module.exports = {
async login(email, password) { async login(email, password) {
if (email === 'Administrator') { if (email === 'Administrator') {
this.session = { this.session = {
user: 'Administrator' user: 'Administrator',
}; };
return; return;
} }
@ -264,9 +275,9 @@ module.exports = {
method: 'POST', method: 'POST',
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json' 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ email, password }) body: JSON.stringify({ email, password }),
}); });
if (response.status === 200) { if (response.status === 200) {
@ -274,7 +285,7 @@ module.exports = {
this.session = { this.session = {
user: email, user: email,
token: res.token token: res.token,
}; };
return res; return res;
@ -288,9 +299,9 @@ module.exports = {
method: 'POST', method: 'POST',
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json' 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ email, fullName, password }) body: JSON.stringify({ email, fullName, password }),
}); });
if (response.status === 200) { if (response.status === 200) {
@ -310,5 +321,5 @@ module.exports = {
if (this.server) { if (this.server) {
this.server.close(); this.server.close();
} }
} },
}; };