2
0
mirror of https://github.com/frappe/books.git synced 2025-02-02 12:08:27 +00:00

more camelCase

This commit is contained in:
Rushabh Mehta 2018-02-08 17:58:51 +05:30
parent dfdb18b5bc
commit bc095b5bc3
24 changed files with 99 additions and 87 deletions

View File

@ -34,7 +34,7 @@ module.exports = class FormPage extends Page {
async show_doc(doctype, name) {
try {
this.doc = await frappe.get_doc(doctype, name);
this.doc = await frappe.getDoc(doctype, name);
this.form.use(this.doc);
} catch (e) {
this.renderError(e.status_code, e.message);

View File

@ -49,7 +49,7 @@ module.exports = class Desk {
})
frappe.router.add('new/:doctype', async (params) => {
let doc = await frappe.get_new_doc(params.doctype);
let doc = await frappe.getNewDoc(params.doctype);
// unset the name, its local
await frappe.router.setRoute('edit', doc.doctype, doc.name);
await doc.set('name', '');

View File

@ -4,12 +4,12 @@ Frappe.js comes in with built-in controls for various types of inputs
## Creating
A new control can be created with `control.make_control` method.
A new control can be created with `control.makeControl` method.
```js
const controls = require('./controls');
let control = controls.make_control({
let control = controls.makeControl({
fieldname: 'test',
fieldtype: 'Data',
label: 'Test Control'
@ -20,7 +20,7 @@ let control = controls.make_control({
The control has the following structure of HTML Elements
- `form_group`
- `formGroup`
- `label`
- `input`
- `description`
@ -46,7 +46,7 @@ Options can be set in the `options` property as a list
Example:
```js
let control = controls.make_control({
let control = controls.makeControl({
fieldname: 'test',
fieldtype: 'Select',
label: 'Test Select',
@ -64,10 +64,25 @@ You can select a value from another DocType with a Link type field. The value of
Example:
```js
let control = controls.make_control({
let control = controls.makeControl({
fieldname: 'user',
fieldtype: 'Link',
label: 'User',
target: 'User'
}, body);
```
## Table
A table control renders a grid object (datatable). The columns of the table are defined by the `childtype` property of the field definition
Example:
```js
let control = controls.makeControl({
fieldname: 'roles',
fieldtype: 'Table',
label: 'Roles',
childtype: 'User Role'
}, body);
```

View File

@ -78,10 +78,10 @@ To setup a form for a new document, just create a new document with the Frappe.j
frappe.router.add('new/todo', async (params) => {
// new document
app.doc = await frappe.get_doc({doctype: 'ToDo'});
app.doc = await frappe.getDoc({doctype: 'ToDo'});
// set a random name
app.doc.set_name();
app.doc.setName();
// show the page
app.edit_page.show();

View File

@ -48,13 +48,13 @@ You can manage documents, using the same Document API as if it were a local data
```js
await frappe.init();
await frappe.init_db('rest', {server: 'localhost:8000'});
await frappe.initDb('rest', {server: 'localhost:8000'});
let doc = await frappe.get_doc({doctype:'ToDo', subject:'test rest insert 1'});
let doc = await frappe.getDoc({doctype:'ToDo', subject:'test rest insert 1'});
await doc.insert();
doc.subject = 'subject changed';
await doc.update();
let data = await frappe.db.get_all({doctype:'ToDo'});
let data = await frappe.db.getAll({doctype:'ToDo'});
```

View File

@ -39,10 +39,10 @@ Lists can be extended by defining a client module for the doctype, similar to fo
const BaseList = require('frappejs/client/view/list');
class ToDoList extends BaseList {
get_fields() {
getFields() {
return ['name', 'subject', 'status'];
}
get_row_html(data) {
getRowHTML(data) {
let symbol = data.status=="Closed" ? "✔" : "";
return `<a href="#edit/todo/${data.name}">${symbol} ${data.subject}</a>`;
}

View File

@ -17,14 +17,11 @@ let todo_list = new Page('ToDo List');
// make the current page active
todo_list.show();
```
```js
// to do list
frappe.router.add('default', () => {
app.todo_list.show();
app.todo_list.list.run();
todo_list.show();
todo_list.list.run();
});
// setup todo form
@ -48,18 +45,18 @@ frappe.router.add('new/todo', async (params) => {
You can change route with
```js
await frappe.router.set_route('list', 'todo');
await frappe.router.setRoute('list', 'todo');
```
## Getting current route
`frappe.router.get_route()` will return the current route as a list.
`frappe.router.getRoute()` will return the current route as a list.
```js
await frappe.router.set_route('list', 'todo');
await frappe.router.setRoute('list', 'todo');
// returns ['list', 'todo'];
route = frappe.router.get_route();
route = frappe.router.getRoute();
```
## Show a route

View File

@ -40,13 +40,13 @@ The `meta` class contains actions that are done on a group of objects and a docu
```js
// extend the meta class
class todo_meta extends frappe.meta.Meta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./todo.json'));
this.name = 'ToDo';
this.list_options.fields = ['name', 'subject', 'status', 'description'];
}
get_row_html(data) {
getRowHTML(data) {
return `<a href="#edit/todo/${data.name}">${data.subject}</a>`;
}

View File

@ -8,11 +8,11 @@ All document write methods are asynchronous and return javascript Promise object
### Initialize
Documents are initialized with the `frappe.get_doc` method. If `doctype` and `name` are passed as parameters, then the document is fetched from the backend. If a simple object is passed, then object properties are set in the document.
Documents are initialized with the `frappe.getDoc` method. If `doctype` and `name` are passed as parameters, then the document is fetched from the backend. If a simple object is passed, then object properties are set in the document.
```js
// make a new todo
let todo = await frappe.get_doc({doctype: 'ToDo', subject: 'something'});
let todo = await frappe.getDoc({doctype: 'ToDo', subject: 'something'});
```
### Create
@ -21,18 +21,18 @@ You can insert a document in the backend with the `insert` method.
```js
// make a new todo
let todo = await frappe.get_doc({doctype: 'ToDo', subject: 'something'});
let todo = await frappe.getDoc({doctype: 'ToDo', subject: 'something'});
await todo.insert();
```
### Read
You can read a document from the backend with the `frappe.get_doc` method
You can read a document from the backend with the `frappe.getDoc` method
```js
// get all open todos
let todos = await frappe.db.get_all({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.get_doc('ToDo', toods[0].name);
let todos = await frappe.db.getAll({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.getDoc('ToDo', toods[0].name);
```
### Update
@ -41,8 +41,8 @@ The `update` method updates a document.
```js
// get all open todos
let todos = await frappe.db.get_all({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.get_doc('ToDo', toods[0].name);
let todos = await frappe.db.getAll({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.getDoc('ToDo', toods[0].name);
first_todo.status = 'Closed';
await first_todo.update();
@ -54,8 +54,8 @@ The `delete` method deletes a document.
```js
// get all open todos
let todos = await frappe.db.get_all({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.get_doc('ToDo', toods[0].name);
let todos = await frappe.db.getAll({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.getDoc('ToDo', toods[0].name);
await first_todo.delete();
```

View File

@ -1,11 +1,11 @@
# Metadata
Metadata are first class objects in Frappe.js. You can get a metadata object by `frappe.get_meta`. All objects from the `models` folders of all modules are loaded.
Metadata are first class objects in Frappe.js. You can get a metadata object by `frappe.getMeta`. All objects from the `models` folders of all modules are loaded.
### Example
```js
let todo_meta = frappe.get_meta('ToDo');
let todo_meta = frappe.getMeta('ToDo');
// get all fields of type "Data"
let data_fields = todo_meta.fields.map(d => d.fieldtype=='Data' ? d : null);

View File

@ -1,19 +1,19 @@
module.exports = {
async init() {
if (this._initialized) return;
this.init_config();
this.init_globals();
this.initConfig();
this.initGlobals();
this._initialized = true;
},
init_config() {
initConfig() {
this.config = {
backend: 'sqlite',
port: 8000
};
},
init_globals() {
initGlobals() {
this.meta_cache = {};
this.modules = {};
this.docs = {};
@ -22,7 +22,7 @@ module.exports = {
}
},
add_to_cache(doc) {
addToCache(doc) {
if (!this.flags.cache_docs) return;
// add to `docs` cache
@ -34,7 +34,7 @@ module.exports = {
}
},
get_doc_from_cache(doctype, name) {
getDocFromCache(doctype, name) {
if (this.docs[doctype] && this.docs[doctype][name]) {
return this.docs[doctype][name];
}
@ -42,12 +42,12 @@ module.exports = {
getMeta(doctype) {
if (!this.meta_cache[doctype]) {
this.meta_cache[doctype] = new (this.getMeta_class(doctype))();
this.meta_cache[doctype] = new (this.getMetaClass(doctype))();
}
return this.meta_cache[doctype];
},
getMeta_class(doctype) {
getMetaClass(doctype) {
doctype = this.slug(doctype);
if (this.modules[doctype] && this.modules[doctype].Meta) {
return this.modules[doctype].Meta;
@ -56,23 +56,23 @@ module.exports = {
}
},
async get_doc(doctype, name) {
let doc = this.get_doc_from_cache(doctype, name);
async getDoc(doctype, name) {
let doc = this.getDocFromCache(doctype, name);
if (!doc) {
let controller_class = this.get_controller_class(doctype);
let controller_class = this.getControllerClass(doctype);
doc = new controller_class({doctype:doctype, name: name});
await doc.load();
this.add_to_cache(doc);
this.addToCache(doc);
}
return doc;
},
new_doc(data) {
let controller_class = this.get_controller_class(data.doctype);
newDoc(data) {
let controller_class = this.getControllerClass(data.doctype);
return new controller_class(data);
},
get_controller_class(doctype) {
getControllerClass(doctype) {
doctype = this.slug(doctype);
if (this.modules[doctype] && this.modules[doctype].Document) {
return this.modules[doctype].Document;
@ -81,16 +81,16 @@ module.exports = {
}
},
async get_new_doc(doctype) {
let doc = frappe.new_doc({doctype: doctype});
async getNewDoc(doctype) {
let doc = this.newDoc({doctype: doctype});
doc.setName();
doc.__not_inserted = true;
this.add_to_cache(doc);
this.addToCache(doc);
return doc;
},
async insert(data) {
return await (this.new_doc(data)).insert();
return await (this.newDoc(data)).insert();
},
login(user='guest', user_key) {

View File

@ -28,7 +28,7 @@ module.exports = {
const BaseDocument = require('frappejs/model/document');
class ${thinname}Meta extends BaseMeta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./${utils.slug(name)}.json'));
}
}

View File

@ -4,12 +4,12 @@ module.exports = {
async get_series_next(prefix) {
let series;
try {
series = await frappe.get_doc('Number Series', prefix);
series = await frappe.getDoc('Number Series', prefix);
} catch (e) {
if (!e.status_code || e.status_code !== 404) {
throw e;
}
series = frappe.new_doc({doctype: 'Number Series', name: prefix, current: 0});
series = frappe.newDoc({doctype: 'Number Series', name: prefix, current: 0});
await series.insert();
}
let next = await series.next()

View File

@ -8,8 +8,8 @@ module.exports = class BaseMeta extends BaseDocument {
this.list_options = {
fields: ['name', 'modified']
};
if (this.setup_meta) {
this.setup_meta();
if (this.setupMeta) {
this.setupMeta();
}
}

View File

@ -2,7 +2,7 @@ const BaseMeta = require('frappejs/model/meta');
const BaseDocument = require('frappejs/model/document');
class NumberSeriesMeta extends BaseMeta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./number_series.json'));
}
}

View File

@ -2,7 +2,7 @@ const BaseMeta = require('frappejs/model/meta');
const BaseDocument = require('frappejs/model/document');
class RoleMeta extends BaseMeta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./role.json'));
}
}

View File

@ -2,13 +2,13 @@ const BaseMeta = require('frappejs/model/meta');
const BaseDocument = require('frappejs/model/document');
class SessionMeta extends BaseMeta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./session.json'));
}
}
class Session extends BaseDocument {
}
module.exports = {

View File

@ -2,7 +2,7 @@ const BaseMeta = require('frappejs/model/meta');
const BaseDocument = require('frappejs/model/document');
class ToDoMeta extends BaseMeta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./todo.json'));
}
}

View File

@ -2,7 +2,7 @@ const BaseMeta = require('frappejs/model/meta');
const BaseDocument = require('frappejs/model/document');
class UserMeta extends BaseMeta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./user.json'));
}
}

View File

@ -2,7 +2,7 @@ const BaseMeta = require('frappejs/model/meta');
const BaseDocument = require('frappejs/model/document');
class UserRoleMeta extends BaseMeta {
setup_meta() {
setupMeta() {
Object.assign(this, require('./user_role.json'));
}
}

View File

@ -27,7 +27,7 @@ module.exports = {
app.post('/api/resource/:doctype', frappe.async_handler(async function(request, response) {
let data = request.body;
data.doctype = request.params.doctype;
let doc = frappe.new_doc(data);
let doc = frappe.newDoc(data);
await doc.insert();
await frappe.db.commit();
return response.json(doc.getValidDict());
@ -36,7 +36,7 @@ module.exports = {
// update
app.put('/api/resource/:doctype/:name', frappe.async_handler(async function(request, response) {
let data = request.body;
let doc = await frappe.get_doc(request.params.doctype, request.params.name);
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
Object.assign(doc, data);
await doc.update();
await frappe.db.commit();
@ -46,7 +46,7 @@ module.exports = {
// get document
app.get('/api/resource/:doctype/:name', frappe.async_handler(async function(request, response) {
let doc = await frappe.get_doc(request.params.doctype, request.params.name);
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
return response.json(doc.getValidDict());
}));
@ -58,7 +58,7 @@ module.exports = {
// delete
app.delete('/api/resource/:doctype/:name', frappe.async_handler(async function(request, response) {
let doc = await frappe.get_doc(request.params.doctype, request.params.name)
let doc = await frappe.getDoc(request.params.doctype, request.params.name)
await doc.delete();
return response.json({});
}));
@ -67,7 +67,7 @@ module.exports = {
app.delete('/api/resource/:doctype', frappe.async_handler(async function(request, response) {
let names = request.body;
for (let name of names) {
let doc = await frappe.get_doc(request.params.doctype, name);
let doc = await frappe.getDoc(request.params.doctype, name);
await doc.delete();
}
return response.json({});

View File

@ -8,7 +8,7 @@ describe('Controller', () => {
});
it('should call controller method', async () => {
let doc = frappe.new_doc({
let doc = frappe.newDoc({
doctype:'ToDo',
subject: 'test'
});

View File

@ -14,7 +14,7 @@ describe('Document', () => {
await doc1.insert();
// get it back from the db
let doc2 = await frappe.get_doc(doc1.doctype, doc1.name);
let doc2 = await frappe.getDoc(doc1.doctype, doc1.name);
assert.equal(doc1.subject, doc2.subject);
assert.equal(doc1.description, doc2.description);
@ -68,10 +68,10 @@ describe('Document', () => {
});
it('should add, fetch and delete documents with children', async() => {
await frappe.new_doc({doctype: 'Role', name: 'Test Role'}).insert();
await frappe.new_doc({doctype: 'Role', name: 'Test Role 1'}).insert();
await frappe.newDoc({doctype: 'Role', name: 'Test Role'}).insert();
await frappe.newDoc({doctype: 'Role', name: 'Test Role 1'}).insert();
let user = frappe.new_doc({
let user = frappe.newDoc({
doctype: 'User',
name: 'test_user',
full_name: 'Test User',
@ -105,7 +105,7 @@ describe('Document', () => {
await user.update();
user = await frappe.get_doc('User', user.name);
user = await frappe.getDoc('User', user.name);
assert.equal(user.roles.length, 1);
assert.equal(user.roles[0].role, 'Test Role 1');
@ -118,7 +118,7 @@ describe('Document', () => {
});
function test_doc() {
return frappe.new_doc({
return frappe.newDoc({
doctype: 'ToDo',
status: 'Open',
subject: 'testing 1',

View File

@ -32,23 +32,23 @@ describe('REST', () => {
});
it('should create a document', async () => {
let doc = frappe.new_doc({doctype:'ToDo', subject:'test rest insert 1'});
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
await doc.insert();
let doc1 = await frappe.get_doc('ToDo', doc.name);
let doc1 = await frappe.getDoc('ToDo', doc.name);
assert.equal(doc.subject, doc1.subject);
assert.equal(doc1.status, 'Open');
});
it('should update a document', async () => {
let doc = frappe.new_doc({doctype:'ToDo', subject:'test rest insert 1'});
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
await doc.insert();
doc.subject = 'subject changed';
await doc.update();
let doc1 = await frappe.get_doc('ToDo', doc.name);
let doc1 = await frappe.getDoc('ToDo', doc.name);
assert.equal(doc.subject, doc1.subject);
});
@ -63,7 +63,7 @@ describe('REST', () => {
});
it('should delete a document', async () => {
let doc = frappe.new_doc({doctype:'ToDo', subject:'test rest insert 1'});
let doc = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 1'});
await doc.insert();
assert.equal(await frappe.db.exists(doc.doctype, doc.name), true);
@ -73,8 +73,8 @@ describe('REST', () => {
});
it('should delete multiple documents', async () => {
let doc1 = frappe.new_doc({doctype:'ToDo', subject:'test rest insert 5'});
let doc2 = frappe.new_doc({doctype:'ToDo', subject:'test rest insert 6'});
let doc1 = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 5'});
let doc2 = frappe.newDoc({doctype:'ToDo', subject:'test rest insert 6'});
await doc1.insert();
await doc2.insert();