- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个应用程序项目,在使用此应用程序时,应在其中离线存储数据(以变量的形式)。
我已在我的应用程序中成功创建了 IndexedDB
数据库,您可以从以下浏览器屏幕截图中看到:
我创建了一个签名板,用户可以在其中绘制签名。我不想立即将签名图像上传到服务器。相反,我想将签名的 base64 字符串存储在 IndexedDB 中。存储后,我想清除签名板并将下一个签名图像作为 Base64 字符串存储在 IndexedDB 中,但避免丢失第一个签名中的字符串。
我使用以下代码创建了数据库。变量imagetxtb64
是签名的base64字符串。
storestring: function () {
var signpad = this.getView().byId("digitalSignatureId");
var imagetxtb64 = signpad.getSignatureAsJpeg();
var image = new Image();
var element = document.createElement('a');
image.src = imagetxtb64;
var request = window.indexedDB.open("Offline Database Example", 1);
request.onupgradeneeded = function(event){
var db = event.target.result;
var objectStore = db.createObjectStore("mydata");
};
}
问题是,我不知道如何将变量imagetxtb64
存储在IndexedDB中。此外,我想稍后访问这些变量。
最佳答案
要将新对象存储在对象存储中,您需要使用 put 或 add 方法。这是一个简短的示例:
function putThing(db, thing, callback) {
// Start a writable transaction on the object store containing things
var transaction = db.transaction('things', 'readwrite');
// Get the handle to the object store in the transaction
var store = transaction.objectStore('things');
// Listen for when the transaction commits (completes) so we can call the
// callback function to let the caller know the operate is done, both when
// it was done successfully, and when an error occurred.
transaction.oncomplete = function(event) {
var error = undefined;
callback(error, thing);
};
transaction.onerror = function(event) {
var error = event.target.error;
callback(error, thing);
};
// Insert or update the thing in the store
store.put(thing);
// extra note: you could also listen to when the request completes with an
// error or successfully, but keep in mind that when making actual changes
// like add/delete/update, your changes really only take place when the
// transaction completes, not the requests. that is why this example only
// cares about when the transaction completes and not the request. be careful,
// a lot of 'toy' examples on the internet make this mistake of prematurely
// concluding the operation was successful by only looking at whether the
// request completed and not the transaction. that shortcut is only correct
// when you are reading data (in a readonly transaction).
}
putThing(db, myThing, function myCallback(error, thing) {
if(error) {
console.log('problem storing thing', error, thing);
} else {
console.log('stored thing!', thing);
}
});
这是一个简单的操作,在 indexedDB 的几篇介绍中都有详细记录。我强烈建议阅读一些introductory documentation 。这也是您的问题存在问题的部分原因,因为这是您在互联网上阅读有关 indexedDB 的任何介绍时可以学到的第一件事。
你的问题的第二部分是你想每次存储一个新的东西。因此,在这种情况下,您可能希望不断向同一家商店添加新产品。
通过将数据存储在数据库中,您稍后可以访问它。稍后您可以随时连接到数据库并从中加载数据。这也是相当琐碎的事情,并且在互联网上几乎所有关于 indexedDB 的介绍中都详细介绍了这一点。
编辑:这是根据您的评论进行的后续行动。
所以你正在存储一个字符串变量,即base 64编码。如果将其用作关键路径,则可以清楚地标识商店中的每个对象。因此,您想确保在第二次执行函数时存储新条目,您可能想存储不同的内容。相反,使用 id 属性。将 id 属性设置为键路径。创建对象存储时在 id 属性上设置自动递增标志。
然后,在存储对象之前不要在对象中设置 id 属性。然后存储它。存储后,它将被分配一个新的 id。每次使用没有键路径的对象调用 put 时,它将被插入。每次您使用带有其键路径的对象调用 put 时,该对象都会被替换。
关于javascript - UI5 索引数据库 : store variables and call them later,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55097882/
我们可以使用 go 进行模块的通用导入吗?更清楚地说,这里是用例: package main import ( "fmt" "net/http" ) json handler(w ht
这是我在 Chrome 控制台中看到的内容: > new Date(2012, 9, 13).toString() "Sat Oct 13 2012 00:00:00 GMT+0800 (CST)"
我有一个 AI 例程,可以将各种数据存储为 Memory 对象。 Memory 对象根据传递给构造函数的“内存类型”具有不同的参数(回想起来,每种类型的内存确实应该是 Memory 的子类,但是目前这
我正在使用later.js我正在尝试在 Meteor 中安排一个帖子。 我什至还没有到达调度部分,因为我无法理解 later.js recur() 如何通过将时间段链接在一起来工作,并执行一个最终会找
有没有办法在 Java 中定义“指向指针的指针”? 即我正在考虑这样的事情...... class Main { public static void main (String[] args)
我的代码中有一个 IIFE,而其他 IIFE 很少。他们都有名字(所以他们不是匿名的)。我想稍后通过其他功能调用一些 IIFE(我的意思是,是的,它们在开始时就完成了它们的工作,但我想稍后再使用它们)
我正在尝试清理 PostgreSQL 表中的数据,其中一些记录在 email_address 列中有大量脏话(结果,这些记录是由激动的用户输入的由于已修复的错误而感到沮丧): ┌───────
我想使用 Postgres 对地址进行一些基本的地理编码。我有一个包含大约 100 万个原始地址字符串的地址表: => \d addresses Table "public.addresses"
我正在创建一个用于每月定期订单的平台。 我正在使用later.js进行重复。我遇到过以下两种情况,我想知道是否有人对如何更好地处理这些情况有建议(或者later.js是否以某种方式本地处理它们): l
这是 fiddle :http://jsfiddle.net/ccarns/hXPU9/ 尝试让月份选项显示下个月的同一天。当前的实现返回每月的第一天。 later.parse.text("every
我想通过添加“稍后提醒我”操作来扩展我的本地通知功能。换句话说,如果用户点击“稍后提醒我”按钮,我想在设定的时间后重新显示通知。 即使我的应用程序中的所有内容都应该正确连接(检查通知是否启用、设置通知
我正在浏览 Later.js 文档,它记录了如何很好地设置时间表 - 但没有说明如何在完成后执行该功能。 如果我有 function logit(){ console.log('it is done'
我使用 Later.JS 在每个月的第一天进行自动函数调用。为了进行测试,我需要模拟时间来检查 Later.JS 和我的代码是否执行正确的操作。有人可以帮助我如何 mock 过去的时光吗? 谢谢 最佳
我想调用一些已实现的验证 Javascript 方法 validatePageProperties = function() {...} 当我单击“稍后激活”按钮时(见图)。知道如何做到这一点吗? 最
通过首先查看 Vim 中的时间旅行命令,即 ea {N}s 和 lat {N}s,我认为它会非常简单。但是,它并没有像预期的那样为我工作,现在我完全困惑了。事情就是这样。 我启动了我的 Vim,并开始
我不知道我在说什么的真正好名字,但是,大多数编程语言都使用类似这样的赋值语句语法: (变量)(赋值运算符)(表达式) 还有一些有一些特殊的赋值运算符,比如var1 += var2等价于var1 = v
我有以下设置: CREATE TABLE A(id INT); CREATE TABLE B(id INT); CREATE TABLE C(aid INT, bid INT); 我想做以下事情: I
在我的 PostgreSQL 9.6.2 数据库中,我有一个查询,它根据一些股票数据构建一个计算字段表。它为表中的每一行计算 1 到 10 年的移动平均窗口,并将其用于周期性调整。具体来说,CAPE、
我在 postgresql 9.5 中使用“LEFT JOIN LATERAL”函数时遇到了一些困难。 在我的表中,有'ID'、'DATE'、'CODE'三列。一个人(ID)有多个行,如下所示。 ID
我正在使用 Postgres 9.3。 我有两个表 T1 和 T2 以及它们之间的 n:m 关系 T1_T2_rel。现在我想创建一个 View ,除了 T1 的列之外,还提供一个列,对于 T1 中的
我是一名优秀的程序员,十分优秀!