Javascript SAPUI5 jQuery.sap.storage变量保存
在我的应用程序中,我有一个用于同步的函数,在该函数中,我在开始和结束时获取两个时间戳,以获取同步时花费的时间 我想把这个变量保存到本地存储器中 之后,我需要将来自函数的变量与来自函数的变量进行比较,得到它们的平均值 我知道存储是一种键值类型,但我仍然无法完成这项工作。该功能发布在下面。谢谢你提供的一切可能的帮助Javascript SAPUI5 jQuery.sap.storage变量保存,javascript,html,local-storage,sapui5,Javascript,Html,Local Storage,Sapui5,在我的应用程序中,我有一个用于同步的函数,在该函数中,我在开始和结束时获取两个时间戳,以获取同步时花费的时间 我想把这个变量保存到本地存储器中 之后,我需要将来自函数的变量与来自函数的变量进行比较,得到它们的平均值 我知道存储是一种键值类型,但我仍然无法完成这项工作。该功能发布在下面。谢谢你提供的一切可能的帮助 handleSyncPress: function() { new Date().getTime(); var syncStart = Math.floor(Date.
handleSyncPress: function() {
new Date().getTime();
var syncStart = Math.floor(Date.now() / 1000);
var that = this;
var fUpdateBindings = function() {
that.getView().getModel().refresh(true);
}
test.mp.Offline.sync(fUpdateBindings);
new Date().getTime();
var syncEnd = Math.floor(Date.now() / 1000);
var syncTime = syncEnd - syncStart;
this._oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
this._oMyData = this._oStorage.get(syncTime);
this._oStorage.put(syncTime, this._oMyData);
}
如您所见,我至少已经开始初始化存储。行
this._oMyData =this._oStorage.get(syncTime);
在你的情况下不会返回任何东西,对吗?这是因为在此调用之前没有存储值。此外,我想你应该用一个字符串作为键
使用SAPUI5访问本地存储的工作原理如下:
// get an instance of jQuery.sap.storage.Storage
var oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
//...
// Store
var syncTime = ...;
oStorage.put("syncTime", syncTime);
// Read
var syncTime = oStorage.get("syncTime");
但是,我更喜欢使用本机JavaScript API,即请参见:
键应该是string…正如我在你的另一个问题的评论中所说,存储类似于存储键值对的字典 密钥是稍后用于访问值的标识符 值可以是任何内容:数字、字符串、布尔值、数组、对象,您可以随意命名 在我看来,最好的解决方案是将所有同步时间存储在一个值中(即一个同步时间数组)
编辑:根据API,仅支持字符串作为值。我尝试了其他类型,它们都能工作,但(反)序列化数据可能是最安全的。我更新了代码示例。我的问题又一次提得很糟糕。我昨天就知道你是对的,而且它是有效的,问题是:我不允许使用这个数组,因为内存会满的。所以最后我不得不处理存储器中的一个变量。最后的错误是打字错误。我本来会删除这个问题,但这已经不可能了。我唯一剩下的问题是,我是否可以直接使用JavaScript类中另一个View.XML中的存储,还是必须检查模型?您可以从应用程序中的任何位置访问存储。即使是在XML类中,也不必将其解析为模型?例如,在TextviewAFAIK中直接使用时,没有XML类。你的意思是风景。您无法访问它,因为它不是模型。但是您可以使用格式化程序,格式化程序随后访问存储。或者,如果存储的对象是模型,则可以将该模型绑定到视图。但不是存储本身。
// Store
var syncTime = ...;
localStorage.setItem("syncTime", syncTime);
// read
var syncTime = localStorage.getItem("syncTime");
handleSyncPress: function() {
// get current timestamp
var syncStart = Date.now();
// do stuff
var fUpdateBindings = function() {
that.getView().getModel().refresh(true);
}
test.mp.Offline.sync(fUpdateBindings);
// get another timestamp
var syncEnd = Date.now();
// diff between the timestamps is the sync time (in milliseconds)
var syncTimeInMilliseconds = syncEnd - syncStart;
this._oStorage = jQuery.sap.storage(jQuery.sap.storage.Type.local);
// load value for the key "syncTimes"
var aSyncTimes = this._oStorage.get("syncTimes");
aSyncTimes = JSON.parse(aSyncTimes); // may not be needed
// if this is the first time you access the key, initialize the value
if (aSyncTimes === null) {
aSyncTimes = [];
}
// append your new sync time
aSyncTimes.push(syncTimeInMilliseconds);
// store your sync time array
aSyncTimes = JSON.stringify(aSyncTimes); // may not be needed
this._oStorage.put("syncTimes", aSyncTimes);
// hopefully you already know how to calculate the avg value from an array of integers
// if not: avg = sum / length
}