我使用” PouchDB”制作了类似Evernote的鸡蛋,可以在线同步脱机数据。


pouchdb.png

1. PouchDB

概述

" PouchDB"是一个开放源代码数据库,其灵感来自面向文档的数据库" Apache CouchDB"。使用PouchDB,数据在脱机时会保存在本地,并且在联机时会在客户端之间同步数据,因此应用程序用户可以访问最新数据,而不必担心使用环境。它支持与CouchDB兼容的数据库。
offline_replication.gif

2.在远程服务器

上安装CouchDB

1
2
brew install couchdb
npm install -g pouchdb-server

3.启动远程服务器

1
pouchdb-server --port 5984

使用浏览器和访问" http://本地主机:5984"
{" express-pouchdb":"欢迎使用!","版本":" 0.10.0"}
显示,表明服务器已安全启动。

4.安装cors以便能够直接复制到CouchDB

1
2
3
4
npm install -g add-cors-to-couchdb
add-cors-to-couchdb
#もしlocalhostじゃないサーバーをしている場合
#add-cors-to-couchdb #{your-serevr-root-url} -u myusername -p mypassword

5.实际使用

1.下载pouchdb js
单击此处下载pouchdb

2.尝试使用indexedDB和remoteServer

复制

将此内容写入

html文件?,

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script _src="pouchdb-3.2.0.min.js"></script>
<script _src="app.js"></script>
<h1>Home#index</h1>                                                                                        
<p><center>[wp_ad_camp_2]</center></p><p>Find me in app/views/home/index.html.erb</p>
<div id="display_stored_data"></div>
<div id="display_localdb"></div>
<%= link_to "get local memo", "javascript:void(0);", class: "getLocalData" %>
<div id="display_remotedb"></div>
<%= link_to "get remote memo", "javascript:void(0);", class: "getRemoteData" %><br>
<%= link_to "show all local memo", "javascript:void(0);", class: "showAllLocalData" %><br>
<ul id="show_all_local_memo"></ul>
<%= link_to "show all remote memo", "javascript:void(0);", class: "showAllRemoteData" %>
<ul id="show_all_remote_memo"></ul>
<%= link_to "sync to remote", "javascript:void(0);", class: "syncDataToRemote" %><br>
<%= link_to "delete local memo", "javascript:void(0);", class: "deleteAllLocalData" %><br>
<%= link_to "delete remote memo", "javascript:void(0);", class: "deleteAllRemoteData" %>

写在

js文件?,

app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
$(function(){
  var user_id = 1;                                                                                        
  var db_name = "documents";
  var remote_db_name = "http://127.0.0.1:5984/documents";
  //PouchDB.debug.enable('*');
  var ts = new Date().getTime().toString();
  var doc = {
    "_id": ts,
    "user_id": user_id,
    "title": "test document title" + new Date().getTime(),
    "body": "test document body" + new Date().getTime()
  };

  PouchDB.destroy(db_name).then(function () {
    return new PouchDB(db_name);
  }).then(function (db) {
    //
    // IMPORTANT CODE STARTS HERE
    //
    db.put(doc).then(function () {
      return db.get(ts);
    }).then(function (doc) {
      document.getElementById('display_stored_data').innerHTML = 'We stored a memo: ' + JSON.stringify(doc);
    });
    var remote_db = new PouchDB(remote_db_name);
    //db.replicate.to(remote_db);
    //db.replicate.from(remote_db);
  });
  //remoteサーバーからデータを取得する
  $(document).on("click",".getRemoteData",function(){
    var remote_db = new PouchDB(remote_db_name);
    remote_db.get(ts,[],function(err, data){
      document.getElementById('display_remotedb').innerHTML = 'We get a remote memo: ' + JSON.stringify(data);
    });
  });

  //indexedDBからデータを取得する
  $(document).on("click",".getLocalData",function(){
    var local_db = new PouchDB(db_name);
    local_db.get(ts,[],function(err, data){
      document.getElementById('display_localdb').innerHTML = 'We get a local memo: ' + JSON.stringify(data);
    });
  });

  //remoteサーバーからデータを全データを取得する
  $(document).on("click",".showAllRemoteData",function(){
    var target = document.getElementById('show_all_remote_memo');
    target.innerHTML = "";
    var remote_db = new PouchDB(remote_db_name);
    remote_db.allDocs({include_docs: true}, function(err, doc) {
      doc.rows.forEach(function(row){
        var content = document.createElement("li");
        content.innerHTML =("<p>user:"+row.doc.user_id+" > "+row.doc.title+"</p><span>"+row.doc.body+"</span>");
        target.appendChild(content);
      });
    });
  });

  //indexedDBからデータを全データを取得する
  $(document).on("click",".showAllLocalData",function(){
    var target = document.getElementById('show_all_local_memo');
    target.innerHTML = "";
    var db = new PouchDB(db_name);
    db.allDocs({include_docs: true}, function(err, doc) {
      doc.rows.forEach(function(row){
        var content = document.createElement("li");
        content.innerHTML =("<p>user:"+row.doc.user_id+" > "+row.doc.title+"</p><span>"+row.doc.body+"</span>");
        target.appendChild(content);
      });
    });
  });

  $(document).on("click",".deleteAllLocalData",function(){
    var db = new PouchDB(db_name);
    db.destroy();
  });

  $(document).on("click",".deleteAllRemoteData",function(){
    var remote_db = new PouchDB(remote_db_name);
    remote_db.destroy();
  });

  $(document).on("click",".syncDataToRemote",function(){
    var local_db = new PouchDB(db_name);
    var remote_db = new PouchDB(remote_db_name);
    local_db.replicate.to(remote_db);
  });
});

3.练习!

  • 加载页面时,
  • Screen Shot 2014-12-24 at 6.41.03 PM.png

  • 单击"显示所有本地备忘录"以获取本地数据库的数据
  • Screen Shot 2014-12-24 at 6.43.01 PM.png

  • 单击"显示所有远程备忘录"以获取远程服务器数据
  • 但是,我还没有将本地数据同步到远程,因此它不会出现。
    Screen Shot 2014-12-24 at 6.43.01 PM.png

  • 点击"同步到远程",然后"显示所有远程备忘录"
  • Screen Shot 2014-12-24 at 6.53.16 PM.png

    我来了。

  • 重新加载页面以查看本地和远程数据
  • Screen Shot 2014-12-24 at 6.54.26 PM.png

  • 再次单击"同步到远程",然后单击"显示所有远程备忘录"
  • Screen Shot 2014-12-24 at 6.55.52 PM.png

    第二条记录也已同步到

    远程。

    6.小结

    这一次,我通过加载页面将测试数据保存到本地,并通过单击将其同步,但是如果它是待办事项或便笺类型的应用程序,请正确地制作表格并使用保存按钮将其保存到本地。如果连接到网络,则可以与服务器同步,如果没有它,则可以将其保存在本地,并且可以制作一个简单的类似Evernote的应用程序。