mirror of
https://github.com/frappe/books.git
synced 2024-12-26 12:28:12 +00:00
Merge pull request #156 from 18alantom/shift-init
refactor: move fjs init boilerplate to fjs
This commit is contained in:
commit
650b9f6191
43
index.js
43
index.js
@ -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(
|
||||||
@ -78,7 +89,7 @@ module.exports = {
|
|||||||
// add to router if client-server
|
// add to router if client-server
|
||||||
this.app.post(
|
this.app.post(
|
||||||
`/api/method/${method}`,
|
`/api/method/${method}`,
|
||||||
this.asyncHandler(async function(request, response) {
|
this.asyncHandler(async function (request, response) {
|
||||||
let data = await handler(request.body);
|
let data = await handler(request.body);
|
||||||
if (data === undefined) {
|
if (data === undefined) {
|
||||||
data = {};
|
data = {};
|
||||||
@ -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();
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user