|
|
+function ensureRef(e){e.$pouchdbRefs||(e.$pouchdbRefs=Object.create(null))}function defineReactive(e,n,i){n in e?e[n]=i:Vue.util.defineReactive(e,n,i)}function bind(e,n,i){var t=[];defineReactive(e,n,t);var c=new PouchDB(i.localdb),o=new PouchDB(i.remoteURL);e.$pouchdbRefs[n]=c,e.$pouchdbRefs[n].push=function(i){var t=(new Date).toISOString();return i._id=t,e[n][t]=i,new Promise(function(e,n){c.put(i).then(function(n){i._rev=n.rev,e(i)}).catch(function(e){i=Object.create(null),n(e)})})},e.$pouchdbRefs[n].update=function(i){var t=i._id;return e[n][t]=i,new Promise(function(e,n){c.put(i).then(function(n){i._rev=n.rev,e(n)}).catch(function(e){n(e)})})},e.$pouchdbRefs[n].delete=function(i){var t=i._id,o=i._rev;return new Promise(function(i,u){c.remove(t,o).then(function(c){Vue.delete(e[n],t),i(c)}).catch(function(e){u(e)})})},c.sync(o,{live:!0,retry:!0}).on("change",function(i){var t=i.change.docs;console.log("change"),t.forEach(function(i){var t=i._id;if(t in e[n]){if(i._deleted)return void Vue.delete(e[n],t);e[n][t]=i}else{var c=e[n];Vue.set(c,t,i)}})}),c.allDocs({include_docs:!0,descending:!0}).then(function(i){var t={};i.rows.forEach(function(e){t[e.id]=e.doc}),defineReactive(e,n,t)}).catch(function(e){})}function install(e){e.mixin(PouchMixin)}var init=function(){var e=this.$options.pouchdb;ensureRef(this);for(var n in e)bind(this,n,e[n])},PouchMixin={init:init,beforeCreate:init};"undefined"!=typeof window&&window.Vue&&install(window.Vue),module.exports=install;
|