在下面的代码中,fetch()和sync()方法没有做任何事情。 我试图看看我的localStorage中的数据如何更新并且方法没有更新它(示例LS字符串在代码中) 我哪里错了?
function makeWorkingLS(collDesc, projDesc, Id, Description, ElapsedSeconds, ElapsedTime, WorkItemType){
//Create observable object from params
var activeTaskObject = kendo.observable ({
client: collDesc,
project: projDesc,
taskId: Id,
description: Description,
elapsedSeconds: ElapsedSeconds,
elapsedTime: ElapsedTime,
comment: WorkItemType
});
// example string in localStorage:
//{"client":"Morken Mindy","project":"Shazbat creation engine","taskId":183,"description":"Create the Shazbat 100% efficiency engine","elapsedSeconds":296803,"elapsedTime":"82h43m","comment":"Task"}
// Convert to JSON string for localStorage
var activeTask = JSON.stringify(activeTaskObject);
console.info(activeTask);
//Write to localStorage
window.localStorage.setItem("activeTask",activeTask);
//Set it as the active datasource for updating to webservice
var activeTaskDS = new kendo.data.DataSource({
transport: {
read: function(options){
taskItem = JSON.parse(localStorage["activeTask"]);
},
update: {
url: remUpd, //url var declared earlier in the process
dataType: "json"
}
},
schema: {
model: {
client: "client",
taskId: "taskId"
},
data: function(){
return taskItem;
}
}
});
activeTaskDS.fetch(function(){
activeTaskDS.data()[0].set("client", "NOBODY");
activeTaskDS.sync();
cosole.log("activeTaskDS.data()[0] : "+activeTaskDS.data()[0]); //should read 'NOBODY' but reads 'Morken Mindy'
});
}
提前致谢, 尼尔。
答案 0 :(得分:1)
我不确定实际上是什么问题,但我必须指出一些重要的事情:
AFAIK,当您自定义任何transport
方法时,您必须将数据传递到options
对象中的回调:
transport: {
read: function(options){
taskItem = JSON.parse(localStorage["activeTask"]);
// Tells the widget to handle that collection
options.success(taskItem);
}
}
在schema.data
中,您似乎想要通过此方法传递数据(如果我错了,请纠正我)。但这种方法不适用于此目的。它仅用于告诉窗口小部件要读取哪个字段(在向其传递字符串的情况下)或从response
读取属性,该属性作为您不使用的参数。检查第二个示例here。因此,这可能不是将taskItem
对象作为数据读取的正确方法;
说到taskItem
对象,它似乎是你的dataSource的基础数据,但它没有定义(至少在你发布的代码片段中)。我的意思是,如果您按照步骤1进行操作,您甚至不需要再从该对象读取。
请告诉我这是否有用,如果您还需要更多信息。