Transform Javascript code to JSON and vice versa

ぃ、小莉子 提交于 2019-12-25 10:49:10

问题


I'm working on a small project in which I will work with dgrid dojo, the dgrid must be customized according to the profile of the person if he's administrator or simple user. The dgrid will be filled from a REST service, and I had the idea to not only fill the dgrid but also customize it with textbox, buttons and checkbox.

For example here is the code of a dgrid with 2 columns whose fields can not be changed:

window.Grid= new StandardGrid({
    sort: "abbreviation",
    store: stateStore(),
    columns: {
           abbreviation: 'Abbreviation',
           name: 'Name'
    }
}, "MyGrid");

the service must verify if the person is an administrator and if so send in JSON Format the settings to add more features to dgrid, by changing "columns" with :

[ editor({
    label: "Abbreviation",
    field: "abbreviation",
    editor: "text",
    editOn: "dblclick"
}),
editor({
    label: "Name",
    field: "name",
    editor: "text",
    editOn: "dblclick"
})]

I want to send this configuration code in json format. I want to know if this is workable and if so, how? Thank you


回答1:


Since functions aren't a valid part of JSON, you'd need to do some amount of processing to go between JSON and dgrid column plugins. For example, in the case of editor, you could just test each object for presence of the editor property, and if found, apply the editor function around it when you actually create the column structure from your JSON.




回答2:


I'm not sure regarding Dojo. But if you want to convert a JS Object to JSON then simply:

var jsonString = JSON.stringify(objectGoesHere);


来源:https://stackoverflow.com/questions/24265557/transform-javascript-code-to-json-and-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!