Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
7355 มุมมอง

How does Odoo parse the options string from a widget in Javascript?

I have a widget with

options = "{'city': 'city', 'street_name': 'street', 'zip': 'zip'}" in the xml

When i write JSON.parse(options) in JS, i get an error: JSON.parse: expected property name or '}'.


But, if i write,

options = '{"city": "city", "street_name": "street", "zip": "zip"}' 

it works fine!


How does Odoo handle this. I want JSON to parse my string irrespective of the quotes..

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Try to parse with `pyUtils`.

it provided some convience function for converting python-like data to js-like.

Search py_eval() / pyeval() in Odoo u should find some examples.



var pyUtils = require('web.py_utils')

// pass some custom options from xml to js _testFunction: function () {   
    var options = pyUtils.py_eval(node.attrs.options)
}


อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

JS Parse replace single quote to double quote globally.

var res = JSON.parse(object.replace(/'/g, '"'));

console.log("city", res.city);

https://stackoverflow.com/questions/36038454/parsing-string-as-json-with-single-quotes



อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ส.ค. 16
4863
2
มี.ค. 15
477
0
มี.ค. 15
4062
0
ก.พ. 21
3219
2
ต.ค. 19
9417