cordova - PouchDB 与 jquery mobile 和 PhoneGap

标签 cordova jquery-mobile pouchdb

我正在尝试在移动(android)应用程序中同时使用上述三个。 当我在 firefox (20)、Ubuntu (12.04) 中运行它时,它运行得很好,但是当我将它加载到手机(或 android 模拟器)中时,它似乎无法存储数据。 在 Firefox 中运行时,数据存储在 sqlite 数据库的用户配置文件目录中,但是使用应用程序时?如何告诉 PouchDB 在哪里存储数据库?我该怎么做?

--附加信息

我在 Firefox 和 Chrome 中对 PouchDB 适配器类型进行了更多测试。 Firefox 不支持 WebSQL,因此使用 chrome 进行测试。 以下是我在 index.html 中加载的脚本。

<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.3.1.min.css" />
<script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery-1.9.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.mobile/pouch-20130419/pouchdb-nightly.js"></script>
<script type="text/javascript" charset="utf-8" src="testa.js"></script>

这是生成这些日志的一些相关代码

console.log("Creating the database" + JSON.stringify(userdata));
Pouch('idb://'+userdata.username, function(err, database){
    if (err) {
    console.log('Failed to create database:'+ JSON.stringify(err));
        return {'ok': false, 'reason':'Failed to create database:'+ userdata.username};
    } else {
        pdb = database;
        console.log('Created database:'+ JSON.stringify(userdata.username));
        Pouch('idb://cred', function(err, credb){
           if (err) {
               console.log('Failed to create database: cred'+ JSON.stringify(err));
               //
               return {'ok': false, 'reason':'Failed to create database: cred'};
           } else {
               pdbcred = credb;
               console.log('Created database: cred');
               ...

在firefox中,控制台日志的输出如下,带有idb适配器 - Pouch("idb://idbx")

testa.js (line 296)     Creating the database{"username":"idbx","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="452c21273d05243621236b24362123" rel="noreferrer noopener nofollow">[email protected]</a>","password":"password"} 
pouchd...htly.js (line 759) idb://idbx 
pouchd...htly.js (line 764) ["idb://idbx","idb","idbx"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"idbx","adapter":"idb"} 
pouchd...htly.js (line 759) idb://_pouch__allDbs 
pouchd...htly.js (line 764) ["idb://_pouch__allDbs","idb","_pouch__allDbs"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"_pouch__allDbs","adapter":"idb"} 
testa.js (line 303)     Created database:"idbx" 
pouchd...htly.js (line 759) idb://cred 
pouchd...htly.js (line 764) ["idb://cred","idb","cred"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"cred","adapter":"idb"} 
pouchd...htly.js (line 759) idb://_pouch__allDbs 
pouchd...htly.js (line 764) ["idb://_pouch__allDbs","idb","_pouch__allDbs"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"_pouch__allDbs","adapter":"idb"} 
testa.js (line 311)     Created database: cred 
testa.js (line 328)     Saved document:{"_id":"idbx","password":"password","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8a1acaab088a9bbacaee6a9bbacae" rel="noreferrer noopener nofollow">[email protected]</a>","savecred":true}:{"ok":true,"id":"idbx","rev":"1-2f0ff07a7c0d8fb6f172a6bfe8a9174f"}

将适配器更改为 websql 并在 chrome 上进行测试 - 工作正常。

将 websql 版本 apk 加载到模拟器中(使用 google API 而不是 4.2.2 api),这就是 logcat 上出现的内容(对于 idb,logcat 是相同的):

04-19 00:28:26.571: I/Web Console(22733): Creating the database{"username":"websqlo","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c7d5d2c3c1dcdff0d1c3d4d69ed1c3d4d6" rel="noreferrer noopener nofollow">[email protected]</a>","password":"password"} at file:///android_asset/www/testa.js:296
04-19 00:28:26.601: I/Web Console(22733): websql://websqlo at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:759
04-19 00:28:26.611: I/Web Console(22733): ["websql://websqlo","websql","websqlo"] at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:764
04-19 00:28:26.631: I/Web Console(22733): "websql" at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:767
04-19 00:28:26.651: I/Web Console(22733): {} at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:768
04-19 00:28:26.751: E/Web Console(22733): Uncaught TypeError: Cannot call method 'valid' of undefined at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:769

pouchdb-nightly 中的条目来 self 添加到 pouch js 文件中的一些 console.log 语句。 js 周围的实际代码如下(必须进行字符串化,因为 logcat 除了纯文本外不会显示任何内容):

Pouch.parseAdapter = function(name) {
  console.log(name);
  var match = name.match(/([a-z\-]*):\/\/(.*)/);
  var adapter;
  if (match) {
    // the http adapter expects the fully qualified name
    console.log(JSON.stringify(match));
    name = /http(s?)/.test(match[1]) ? match[1] + '://' + match[2] : match[2];
    adapter = match[1];
    console.log(JSON.stringify(adapter));
    console.log(JSON.stringify(Pouch.adapters))
    if (!Pouch.adapters[adapter].valid()) {
      throw 'Invalid adapter';
    }
    console.log(JSON.stringify({name: name, adapter: match[1]}));
    return {name: name, adapter: match[1]};
  }

非常感谢有关解决此问题的任何帮助/建议。感谢 chesles 和经常提出的建议 - 我现在就去看看它们。 仅供引用 - 像 Pouch("idb://testpath/testdb") 这样的 url 不起作用...提示元数据。 在我脑海深处的某个地方,我在想应该有一种方法来告诉 PouchDB 在哪里存储文件(尤其是在应用程序中)以及有关目录的读/写权限的信息。也许我应该寻找其他地方......欢迎所有帮助。

最佳答案

这里有几点需要注意:

  1. 如果您只需为 PouchDB 提供一个不带 adapter:// 前缀的数据库名称,PouchDB 将使用适用于平台的最佳适配器。如果您想使用本地存储适配器,这可能是创建数据库的最佳方法,因为无论您在什么浏览器上运行,都可以使用相同的代码;只要它支持 WebSQL 或 IndexedDB,它就应该可以正常工作。

  2. Android 版 PhoneGap 的跟踪支持存在一个 Unresolved 问题 here 4 天前关闭,现已确认正常运行。不幸的是,我现在无法亲自确认这一点,但它应该有效。

  3. 查看 howonlee 关于拉取请求的注释 670 ,特别是关于将所有内容包装在 deviceready 回调中:

    Note that we must create and use the db inside an onDeviceReady() function, because we use websql which only becomes available to us after the deviceready event.

关于cordova - PouchDB 与 jquery mobile 和 PhoneGap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16053776/

相关文章:

javascript - 为什么 js 变量在桌面上的 JQuery 移动多页面模板页面上持久存在,但在移动设备上却不然

couchdb - 如何解决与连续复制的冲突

javascript - Angular.js 在 DOM 准备好之前显示加载动画

ios - 在 iOS 上的 View 之间转换期间闪烁( ionic 框架)

ios - 无法在 iOS 中安装 Plugman

jquery - 使 Web 应用程序与掌上电脑兼容

javascript - pageshow 上的 Jquery 移动 ListView css

javascript - 文档之间的连接查询

node.js - 在 CouchDB 中实现 "Database per user pattern"(Cloudant <-> PouchDB)

node.js - Phonegap : two kinds of installations, 有什么区别?