2013-07-02 46 views
0

我有一个JavaScript里面有一堆参数和函数。从json字符串传递复杂的javascript对象配置(数据和函数)

ctrl.kendoGrid({ 
dataSource: { 
    type: "odata", 
    transport: { 
     read: { 
      url: "odata/CodeView", 
      dataType: "json", 
      contentType: "application/json" 
     }, 
     update: { 
      url: function (data) { 
       return "api/CodeMapUpdate/" + data.CODE_MAP_ID; 
      }, 
      dataType: "json", 
      type: "post", 
      complete: function (e) { 
       ctrl.data("kendoGrid").dataSource.read(); 
       if (e.status == 201) { 
        logger.log("Record Updated: Record ID = " + e.responseJSON, null, null, true); 
       } else { 
        logger.logError(" Save failed " + e.responseJSON.ExceptionMessage, null, null, true); 
       } 

      } 
     }, 
     destroy: { 
      url: function (data) { 
       return "api/CodeMapDelete/" + data.CODE_MAP_ID; 
      }, 
      dataType: "json", 
      complete: function() { 
       ctrl.data("kendoGrid").dataSource.read(); 
      } 
     }, 
     create: { 
      url: "api/CodeMapCreate", 
      dataType: "json", 
      complete: function (e) { 
       ctrl.data("kendoGrid").dataSource.sort({ 
        field: "CODE_MAP_ID", 
        dir: "desc" 
       }); 
       ctrl.data("kendoGrid").dataSource.filter({}); 
       if (e.status == 201) { 
        logger.log("Record Created: Record ID = " + e.responseJSON, null, null, true); 
       } else { 
        logger.logError(" Save failed " + e.responseJSON.ExceptionMessage, null, null, true); 
       } 

      } 
     }, 
    }, 
    schema: { 
     data: function (data) { 
      return data.value; 
     }, 
     total: function (data) { 
      return data["odata.count"]; 

     }, 
     model: { 
      id: "CODE_MAP_ID", 
      fields: { 
       CODE_MAP_ID: { 
        editable: false, 
        type: "number" 
       }, 
       CODE_NAME: { 
        type: "string", 
        validation: { 
         title: "Required Field", 
         required: true 
        } 
       }, 
       L_NAME: { 
        type: "string", 
        validation: { 
         required: true 
        } 
       }, 
       CODE_DATE: { 
        field: "CODE_DATE", 
        type: "date", 
        format: "{0:MM/dd/yyyy}", 
        validation: { 
         required: true 
        } 
       }, 
      } 
     } 
    }, 
    change: function() { 

    }, 
    //batch: true, 
    pageSize: 20, 
    serverPaging: true, 
    serverFiltering: true, 
    serverSorting: true, 
    sort: { 
     field: "CODE_MAP_ID", 
     dir: "desc" 
    } 
    //autoSync: true 
} }) 

我想将整个“dataSource”对象保存为一个变量,并在运行时用ajax检索它。
我能够用eval(“(”+ dataSource +“)”)做到这一点,但任何包含的函数不再执行。

任何想法的存储/检索这种类型的对象/从JSON的战略?

+0

Function.prototype.toJSON = Function.toString; ,使用regexp验证parse()。 – dandavis

+0

请你详细说明一下,也许一些样品代码 – LastTribunal

回答

1

这里是如何使用第二个参数去JSON.parse恢复存储功能的一个简单的演示:

var ob={a:1, b:false, c: function(a){ return a * a;} };//a sample object 

Function.prototype.toJSON=Function.toString; //extend JSON to cover Functions 

var str=JSON.stringify(ob, null, "\t"); //turn sample object into a string: 
/* str =={ 
    "a": 1, 
    "b": false, 
    "c": "function (a){return a*a;}" 
} */ 


//now turn the string back into an object, using a reviver to re-parse methods: 
var ob2=JSON.parse(str, function(a,b){ 
    if(b.match && b.match(/^function[\w\W]+\}$/)){ b=eval("b=0||"+b); } 
    return b; 
}); 


var n=5; //let's try the method using a number 
var n2=ob2.c(5); //apply the method to the number 
alert(n2); // shows: 25, the number times itself, verifying that the function works 

你可能想成为痘痘严格,将你送什么给eval,也许使用除了匹配看起来像函数的属性之外的关键模式。你可以加强正则表达式来加强一点,但是对于JSON.parse()参数的这个快速演示,它一切正常。

在这种情况下,由于您正在收集JSON的属性,因此不会遇到eval()使用可能会促成的安全问题。这些问题源于将一个用户的输入发送给另一个用户而不进行过滤,而不是当您上一次生成的客户端自己的代码跳转时启动代码...

+0

非常感谢丹。 – LastTribunal

相关问题