1

我是 PouchDB 的新手,目前尝试在我的 Phonegap android 应用程序上使用 Pouchdb。我正在使用来自http://pouchdb.com/getting-started.html的 Todos 示例应用程序。

它在浏览器上运行良好,但在 Android(Jelly Bean 4.2)上运行良好,我正在使用 HTC One Mini 来测试应用程序。

这是我的代码:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>VanillaJS TodoMVC</title>
    <link rel="stylesheet" href="style/base.css">
    <!--[if IE]>
      <script src="style/ie.js"></script>
    <![endif]-->
    <script src="js/jquery-1.10.2.min.js" type="text/javascript"></script>


  </head>
  <body>
    <section id="todoapp">
      <header id="header">
    <h1>todos</h1>
    <input id="new-todo" placeholder="What needs to be done?" autofocus value="">
      </header>
      <section id="main">
    <ul id="todo-list"></ul>
      </section>
      <footer id="footer">
    <span id="todo-count"></span>
        <div id="sync-wrapper">
          <div id="sync-success">Currently syncing</div>
          <div id="sync-error">There was a problem syncing</div>
        </div>
      </footer>
    </section>
    <script src="js/pouchdb-nightly.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/base.js"></script>
  </body>
</html>

这是 app.js 代码:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){}    


(function() {

  'use strict';

  var ENTER_KEY = 13;
  var newTodoDom = document.getElementById('new-todo');
  var syncDom = document.getElementById('sync-wrapper');

  // EDITING STARTS HERE (you dont need to edit anything above this line)

  var db = new PouchDB('todos');
  var remoteCouch = false;

  db.info(function(err, info) {
    db.changes({
        since: info.update_seq,
        continuous: true,
        onChange: showTodos
    });
  });

  // We have to create a new todo document and enter it in the database
  function addTodo(text) {
      var todo = {
        _id: new Date().toISOString(),
        title: text,
        completed: false
      };

      db.put(todo, function callback(err, result) {
        if(!err) {
            console.log('Successfully posted a todo!');
        }
      });
  }

  // Show the current list of todos by reading them from the database
  function showTodos() {
      db.allDocs({include_docs: true, descending:true}, function(err,doc) {
          redrawTodosUI(doc.rows);
      });
  }

  function checkboxChanged(todo, event) {
      todo.completed = event.target.checked;
      db.put(todo);
  }

  // User pressed the delete button for a todo, delete it
  function deleteButtonPressed(todo) {
      db.remove(todo);
  }

  // The input box when editing a todo has blurred, we should save
  // the new title or delete the todo if the title is empty
  function todoBlurred(todo, event) {
      var trimmedText = event.target.value.trim();

      if(!trimmedText) {
        db.remove(todo);
      } else {
        todo.title = trimmedText;
        db.put(todo);
      }
  }

  // Initialise a sync with the remote server
  function sync() {
  }

  // EDITING STARTS HERE (you dont need to edit anything below this line)

  // There was some form or error syncing
  function syncError() {
    syncDom.setAttribute('data-sync-state', 'error');
  }

  // User has double clicked a todo, display an input so they can edit the title
  function todoDblClicked(todo) {
    var div = document.getElementById('li_' + todo._id);
    var inputEditTodo = document.getElementById('input_' + todo._id);
    div.className = 'editing';
    inputEditTodo.focus();
  }

  // If they press enter while editing an entry, blur it to trigger save
  // (or delete)
  function todoKeyPressed(todo, event) {
    if (event.keyCode === ENTER_KEY) {
      var inputEditTodo = document.getElementById('input_' + todo._id);
      inputEditTodo.blur();
    }
  }

  // Given an object representing a todo, this will create a list item
  // to display it.
  function createTodoListItem(todo) {
    var checkbox = document.createElement('input');
    checkbox.className = 'toggle';
    checkbox.type = 'checkbox';
    checkbox.addEventListener('change', checkboxChanged.bind(this, todo));

    var label = document.createElement('label');
    label.appendChild( document.createTextNode(todo.title));
    label.addEventListener('dblclick', todoDblClicked.bind(this, todo));

    var deleteLink = document.createElement('button');
    deleteLink.className = 'destroy';
    deleteLink.addEventListener( 'click', deleteButtonPressed.bind(this, todo));

    var divDisplay = document.createElement('div');
    divDisplay.className = 'view';
    divDisplay.appendChild(checkbox);
    divDisplay.appendChild(label);
    divDisplay.appendChild(deleteLink);

    var inputEditTodo = document.createElement('input');
    inputEditTodo.id = 'input_' + todo._id;
    inputEditTodo.className = 'edit';
    inputEditTodo.value = todo.title;
    inputEditTodo.addEventListener('keypress', todoKeyPressed.bind(this, todo));
    inputEditTodo.addEventListener('blur', todoBlurred.bind(this, todo));

    var li = document.createElement('li');
    li.id = 'li_' + todo._id;
    li.appendChild(divDisplay);
    li.appendChild(inputEditTodo);

    if (todo.completed) {
      li.className += 'complete';
      checkbox.checked = true;
    }

    return li;
  }

  function redrawTodosUI(todos) {
    var ul = document.getElementById('todo-list');
    ul.innerHTML = '';
    todos.forEach(function(todo) {
      ul.appendChild(createTodoListItem(todo.doc));
    });
  }

  function newTodoKeyPressHandler( event ) {
    if (event.keyCode === ENTER_KEY) {
      addTodo(newTodoDom.value);
      newTodoDom.value = '';
    }
  }

  function addEventListeners() {
    newTodoDom.addEventListener('keypress', newTodoKeyPressHandler, false);
  }

  addEventListeners();
  showTodos();

  if (remoteCouch) {
    sync();
  }

})();

问题:当我添加一个待办事项时,按回车后..它没有出现任何东西..我想知道我的android手机中的本地数据库还没有创建。

希望有人可以帮助我..任何答案将不胜感激!

谢谢

4

2 回答 2

0

我的项目中也有同样的问题。它在 android 版本 4.0.3 上运行良好,但在 4.1 和 4.2.2 上运行良好我在互联网上搜索了很多,发现问题出在新的 android 平台上,而不是在 pouchdb 中。如果你真的想在本地存储上工作,只需使用 IndexedDB

请参阅此链接https://github.com/daleharvey/pouchdb/issues/504

于 2013-12-15T08:40:45.103 回答
0

在 PouchDB 使用 4.2 之前的 Android 版本时,我遇到了两个问题。

  1. 不支持 atob 和 btoa 函数。不过,我最终没有使用它们,请原谅我,我不记得我是如何评论/砍掉它们的。我当然没有用垫片代替它们。

  2. 由于某种原因,indexdb、leveldb、websql 首选项顺序对我不起作用。我不得不特别说使用 websql。我看到一些代码检测到您是否正在运行phonegap/cordova,然后使用websql(在Android 上转换为sqllite),但由于某种原因也不起作用。

希望有帮助!

于 2014-03-16T07:15:33.587 回答