将Javascript代码转换为JSON,反之亦然 [英] Transform Javascript code to JSON and vice versa

查看:175
本文介绍了将Javascript代码转换为JSON,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个小型项目,我将与dgrid dojo一起工作,如果他是管理员或简单的用户,那么dgrid必须根据该人员的个人资料进行定制。
dgrid将从REST服务中填充,我的想法不仅是填充dgrid,还可以使用文本框,按钮和复选框进行自定义。



例如,这里是一个dgrid的代码,其中2列的字段不能被更改:

  window.Grid = new StandardGrid ({
sort:缩写,
store:stateStore(),
列:{
缩写:'缩写',
名称:'名称'
}
},MyGrid);

该服务必须验证该人员是否为管理员,如果是,则以JSON格式发送设置以添加更多的功能,dgrid,通过更改列与:

   

我想以json格式发送配置代码。
我想知道这是否可行,如果是这样,怎么办?
谢谢

解决方案

由于功能不是JSON的有效部分,您需要做一些数量在JSON和dgrid列插件之间进行处理。例如,在编辑器的情况下,您可以测试每个对象以存在编辑器属性,如果发现,当您实际从JSON中创建列结构时,应用编辑器函数。


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

解决方案

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.

这篇关于将Javascript代码转换为JSON,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆