服务器调用(AJAX)

frappe.call

frappe.call(method, args)

向服务器发起一个 AJAX 请求,其中 method 是一个点分路径,指向一个
已列入白名单的 Python 方法,该方法会被执行,其返回值将作为
响应发送回来。

// call with no parameters
frappe.call('ping')
 .then(r => {
 console.log(r)
 // {message: "pong"}
 })

// call with a single parameter
frappe.call('frappe.core.doctype.user.user.get_role_profile', {
 role_profile: 'Test'
}).then(r => {
 console.log(r.message)
})

// call with all options
frappe.call({
 method: 'frappe.core.doctype.user.user.get_role_profile',
 args: {
 role_profile: 'Test'
 },
 // disable the button until the request is completed
 btn: $('.primary-action'),
 // freeze the screen until the request is completed
 freeze: true,
 callback: (r) => {
 // on success
 },
 error: (r) => {
 // on error
 }
})

frappe.db.get_doc

frappe.db.get_doc(doctype, name, filters)

返回 doctypename 对应的文档对象。如果未提供 name
则获取由 filters 匹配到的第一个文档。

// get doc by name
frappe.db.get_doc('Task', 'TASK00002')
 .then(doc => {
 console.log(doc)
 })

// get doc by filters
frappe.db.get_doc('Task', null, { status: 'Open' })
 .then(doc => {
 console.log(doc)
 })

frappe.db.get_list

frappe.db.get_list(doctype, { fields, filters })

返回 doctype 的记录列表,并应用 fieldsfilters 条件。

frappe.db.get_list('Task', {
 fields: ['subject', 'description'],
 filters: {
 status: 'Open'
 }
}).then(records => {
 console.log(records);
})

frappe.db.get_value

frappe.db.get_value(doctype, name, fieldname)

返回文档的字段值或值列表。

// single value
frappe.db.get_value('Task', 'TASK00004', 'status')
 .then(r => {
 console.log(r.message.status) // Open
 })

// multiple values
frappe.db.get_value('Task', 'TASK00004', ['status', 'subject'])
 .then(r => {
 let values = r.message;
 console.log(values.status, values.subject)
 })

// using filters
frappe.db.get_value('Task', {status: 'Open'}, 'subject')
 .then(r => {
 let values = r.message;
 console.log(values.subject)
 })

frappe.db.get_single_value

frappe.db.get_single_value(doctype, field)

从单一文档类型(Single DocType)中返回一个字段值。

frappe.db.get_single_value('System Settings', 'time_zone')
 .then(time_zone => {
 console.log(time_zone);
 })

frappe.db.set_value

frappe.db.set_value(doctype, docname, fieldname, value, callback)

在服务器端使用 frappe.get_docdoc.save 设置文档的属性。

// update a field's value
frappe.db.set_value('Task', 'TASK00004', 'status', 'Open')
 .then(r => {
 let doc = r.message;
 console.log(doc);
 })

// update multiple fields
frappe.db.set_value('Task', 'TASK00004', {
 status: 'Working',
 priority: 'Medium'
}).then(r => {
 let doc = r.message;
 console.log(doc);
})

frappe.db.insert

frappe.db.insert(doc)

插入一个新文档。

frappe.db.insert({
 doctype: 'Task',
 subject: 'New Task'
}).then(doc => {
 console.log(doc);
})

frappe.db.count

frappe.db.count(doctype, filters)

返回给定 doctypefilters 条件下的记录数量。

// total number of Task records
frappe.db.count('Task')
 .then(count => {
 console.log(count)
 })

// total number of Open Tasks
frappe.db.count('Task', {
 filters: {
 status: 'Open'
 }
}).then(count => {
 console.log(count);
})

frappe.db.delete_doc

frappe.db.delete_doc(doctype, name)

删除由 doctypename 标识的文档。

frappe.db.delete_doc('Task', 'TASK00004')

frappe.db.exists

frappe.db.exists(doctype, name)

如果文档记录存在,则返回 true。

frappe.db.exists('Task', 'TASK00004')
 .then(exists => {
 console.log(exists) // true
 })