浏览代码

Initial Commit

sadick254 8 年之前
当前提交
5754ddc0c2
共有 1 个文件被更改,包括 60 次插入0 次删除
  1. 60 0
      vuepouch.js

+ 60 - 0
vuepouch.js

@@ -0,0 +1,60 @@
+var init = function(){
+  var bindings = this.$options.pouchdb
+  ensureRef(this)
+  for(var key in bindings) {
+    bind(this,key,bindings[key])
+  }
+}
+function ensureRef (vm) {
+	  if (!vm.$pouchdbRefs) {
+	    vm.$pouchdbRefs = Object.create(null)
+	  }
+}
+function defineReactive(vm,key,val){
+  if(key in vm){
+    vm[key] = val
+  } else {
+    Vue.util.defineReactive(vm,key,val)
+  }
+}
+function bind(vm,key,source){
+  var array = []
+  defineReactive(vm,key,array)
+  vm.$pouchdbRefs[key] = source
+  source.changes({
+    since:'now',
+    live: true,
+    include_docs: true
+  }).on('complete',function(info){
+    
+  }).on('change',function(change){
+    vm[key].push(change.doc)
+
+  }).on('err',function(err){
+    
+  })
+  source.allDocs({
+        include_docs: true,
+        descending: true
+      }).then(function(doc){
+        var docs = doc.rows.map(function(obj){
+            return obj.doc
+        })
+        defineReactive(vm,key,docs)
+      }).catch(function(err){
+
+      })
+}
+var PouchMixin = {
+  init: init, // making it usable with vuejs 1.x.x
+  beforeCreate: init
+}
+
+function install(Vue){
+  Vue.mixin(PouchMixin)
+}
+
+	// auto install
+	if (typeof window !== 'undefined' && window.Vue) {
+	  install(window.Vue)
+	}