odoojs-rpc
A Javascript Lib to connet odoo. Used to Create Web Application.
contact us: odoojs@outlook.com
handbook
- go https://www.npmjs.com/package/odoojs?activeTab=code
- the folder: test_rpc
- all files in test_rpc
Usage
init
- before use odoojs. should init.
- then use api anywhere
import rpc from 'odoojs'
export const baseURL = 'http://127.0.0.1:8069'
export const timeout = 50000
export function messageError(error) {
console.log('axios error: ', error.data.message)
}
rpc.init({ baseURL, timeout, messageError })
connect odoo, get version_info
import rpc from 'odoojs'
async function test_version_info() {
const result = await rpc.web.webclient.version_info()
console.log(result)
}
test_version_info()
// here is function result
const result = {
server_version: '16.0-20230213',
server_version_info: [16, 0, 0, 'final', 0, ''],
server_serie: '16.0',
protocol_version: 1
}
login odoo
import rpc from 'odoojs'
async function test_login() {
const db = 'odoojs'
const login = 'admin'
const password = 'odoojs'
const kw = { db, login, password }
const info = await rpc.web.login(kw)
const result = await rpc.session_check()
await rpc.web.logout()
}
model api. CRUD
async function test_model() {
const ptn = rpc.env.model('res.partner')
const fields_info = await ptn.fields_get(
['name', 'is_company', 'parent_id', 'child_ids', 'category_id'],
['name', 'type']
)
const records = await ptn.search_read({
domain: [['name', 'like', 'Admin']],
fields: ['name', 'ref', 'is_company', 'child_ids', 'category_id']
})
const ids = await ptn.search([['name', 'like', 'Admin']])
const res_id = ids[0]
const record = await ptn.read_one(res_id, { fields: ['name', 'ref'] })
const write_result = await ptn.write(res_id, { ref: 'ref_code' })
const res_id2 = await ptn.create({ name: 'partner_name' })
const unlink_result = await ptn.unlink(res_id2)
}
api of odoojs model
const partner_obj = rpc.env.model('res.partner')
partner_obj.search_read({domain, fields})
partner_obj.read(res_id, fields)
partner_obj.onchange(...)
partner_obj.create(vals_or_valslist)
partner_obj.write(res_id, vals)
partner_obj.unlink(res_id_or_ids)
partner_obj.name_search
partner_obj.name_get
env api.
rpc.env.model(model_name)
rpc.env.ref(xml_ref)
rpc.treeview(action_id)
rpc.formview(action_id)
api of odoojs viewmodel
// prepare action info and view info
// then
const treeview = rpc.env.treeview(action_id)
await treeview.load_fields()
const result = await treeview.search_read()
const formview = rpc.env.formview(action_id)
await formview.load_fields()
const record = await formview.read(res_id)
const value = {}
const value_display = formview.set_editable({record, value})
const {values} = await formview.onchange(fieldname, value)
const res_id = await formview.commit()
api of odoojs viewmodel one2many field
const formview = rpc.env.formview(action_id)
await formview.load_fields()
const fields_info = formview.fields
const record = await formview.read(res_id)
const relation_fieldinfo = fields_info[relation_field_name]
const o2m_ids = record[relation_field_name]
const relation = rpc.env.relation(relation_fieldinfo)
const o2mtree = relation.tree
const records = await o2mtree.read(o2m_ids)
o2mtree.pick_one
const o2mform = relation.form
const {values} = await o2mform.onchange()
o2mform.commit()
o2mtree.upinsert_one
await formview.onchange(relation_field_name, value)
await formview.commit()