簡體   English   中英

從json字符串傳遞復雜的javascript對象配置(數據和函數)

[英]Pass complex javascript object configuration(data and functions) from json string

我有一個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中/從JSON中檢索這種策略有任何想法嗎?

這是一個有關如何將第二個參數用於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的內容更加嚴格,除了匹配看起來像函數的屬性之外,還可以使用鍵架構。 您可以對regexp進行更嚴格的處理,但是對於JSON.parse()參數的此快速演示,一切都很好。

在這種情況下,由於您要收集JSON的屬性,因此沒有機會碰到eval()可以解決的安全問題。 這些問題源於將一個用戶的輸入發送給另一用戶而不進行過濾,而不是當您啟動客戶端上次生成的代碼時...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM