简体   繁体   中英

Transform Javascript code to JSON and vice versa

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

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

var jsonString = JSON.stringify(objectGoesHere);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM