Kendo Ui DataSource Add Function not working properly

我的梦境 提交于 2019-12-03 17:17:41

It is actually a combined problem:

You schema definition is:

schema: {
    model: {
        extappId: "ID",
        fields: {
            extappId: { type: "number" },
            desc: { type: "string" },
            deviceNo: { type: "number" },
            validationKey: { type: "string" }
        }
    }
 }

It says extappId: "ID"... what is this? Do you want to say that extappId is the id of that record? If so, the actual definition should be:

schema: {
    model: {
        id: "extappId",
        fields: {
            extappId: { type: "number" },
            desc: { type: "string" },
            deviceNo: { type: "number" },
            validationKey: { type: "string" }
        }
    }
 }

What happens here is not being (correctly) defined the id, KendoUI expects that id is actually id and since this is not being set during the create next time that you sync it tries to save it, it is equivalent to:

datasourceAppList.add({ extappId: 9905, desc: "test", deviceNo: 5, validationKey: "CACACACA"});
datasourceAppList.add({ extappId: 9908, desc: "harvest", deviceNo: 7, validationKey: "ppppppp"});
datasourceAppList.sync();

But if you try this, you will see that then create is never invoked. Now what happens is that the id of the record (i.e. extappId) is already defined and not null and so KendoUI believes that is already saved.

For solving this second issue my recommendation is defining the schema as:

schema: {
    model: {
        id: "ID",
        fields: {
            extappId: { type: "number" },
            desc: { type: "string" },
            deviceNo: { type: "number" },
            validationKey: { type: "string" }
        }
    }
 }

Now, the id is a field called ID and this ID should be created on transport.create (cannot be set before invoking add method).

Then, you set it in create and you can use:

create: function(options){
    // Set `ID` to kendo.guid
    options.data.ID = kendo.guid();
    alert(options.data.desc);
    var localData = JSON.parse(localStorage.getItem("LsAppList"));
    localData.push(options.data);
    localStorage.setItem("LsAppList", JSON.stringify(localData)); //localStorage["LsAppList"] = JSON.stringify(localData); 
    options.success(localData);
},

Check it here : http://jsfiddle.net/OnaBai/n7sNd/3/

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