2014-09-30 82 views
1

我怎么能告诉鸡等待异步鸡蛋数据库做聚合物中的鸡功能read()?我的loadChanged不起作用?聚合物indexedDB鸡内鸡蛋

<polymer-element name="my-chicken"> 
    <template> 
     <style> 
      p {font-family: 'RobotoDraft', sans-serif; padding:10px;} 
     </style> 

     <p>Loading...</p> 

     <my-egg iLoad="{{load}}" iDB="{{db}}"></my-egg> 

    </template> 
    <script> 
     Polymer('my-chicken', { 

      load:false, 
      db:null, 
      out:null, 

      write:function(){ 
       this.db.result.transaction(["store"], "readwrite").objectStore("store"); 
       var customerData = [ 
        {muts: "Bill", qty: "1"}, 
        {muts: "Donna", qty: "1"}, 
       ] 
       for (var i in customerData){db.put(customerData[i])} 
      }, 

      remove:function(k){ 
       this.db.result.transaction(["store"], "readwrite").objectStore("store"); 
       db.delete(k) 
      }, 

      read:function(tx){ 
       var f =function(e) { 
       var cursor = e.currentTarget.result; 
       if (cursor) { 
        console.log(cursor.key) 
        var br=document.createElement('br') 
        var span=document.createElement('span') 
        span.innerHTML=cursor.key 
        span.onclick=function(){remove(cursor.key)} 
        this.out.appendChild(span) 
        this.out.appendChild(br) 
        cursor.continue() 
       } 
       }.bind(this) 
       this.out.innerHTML="" 
       var req = tx.openCursor() 
       req.onsuccess = f 
       console.log('hello') 
      }, 

      ready:function(){ 
       this.out=this.shadowRoot.querySelector('p') 
      } 

      loadChanged:function(){ 
       console.log('hello') 
       var tx=this.db.result.transaction(["store"], "readwrite").objectStore("store"); 
       this.read(tx) 
      }  
     }) 
    </script> 
</polymer-element> 

<polymer-element name="my-egg" attributes="iDB iLoad"> 
    <script> 
     Polymer('my-egg', { 
      iDB:null, 
      iLoad:false, 
      ready:function(){ 

       var load=function(){ 
        this.iLoad=true 
        console.log('iLoad',this.iLoad) 
       }.bind(this) 

       var request = indexedDB.open("my-database",1); 
       request.onupgradeneeded = function(e) { 
        var db = e.currentTarget.result 
        var store = db.createObjectStore("store", {keyPath: "muts", autoIncrement:false}) 
        console.log('db upgrade', 'v'+db.version) 
       } 
       request.onerror = function(e) { 
        console.error('db error ',e) 
       } 
       request.onsuccess = function(e) { 
        var db = e.currentTarget.result 
        console.log('db setup', 'v'+db.version, 'OK') 
        load() 
       } 
       this.iDB=request 
      }, 
      drop: function() { 
       this.iDB.result.close() 
       var req = indexedDB.deleteDatabase(this.iDB.result.name); 
       req.onsuccess = function() {console.log("Deleted database successfully")} 
       req.onerror = function() {console.log("Couldn't delete database")} 
       req.onblocked = function() {console.log("Couldn't delete database due to the operation being blocked")} 
      } 
     }) 
    </script> 
</polymer-element> 
+0

首先是谁?我的鸡肉还是我的鸡蛋? – StriplingWarrior 2014-09-30 22:57:56

+0

我的鸡蛋在鸡肉里 – 2014-09-30 23:00:34

+0

但鸡蛋里有鸡吗? – StriplingWarrior 2014-09-30 23:44:42

回答

3

简答题?你不要等待。让你的代码简单地设置你的元素,这样如果没有写入任何东西,那么用户可见的状态就是合理的。然后只要你的“蛋”完成它的事情就更新它。如果该更新触发计数器更新,那么这非常好,并且您再次在用户可见状态下反映出来。

另外:可爱,因为它是最糟糕的元素命名为了得到有用的答案的目的。评论主题几乎完全脱轨,因为它=)

+0

我编辑了这个问题。我现在被困在负载监视器this.iLoad中。不知道如何从蛋元中得到另一个“这个”。 – 2014-10-01 02:54:49

+1

只是...别名,就像你会在其他地方做的一样? 'var self = this; var load = function(){self.iLoad = true; }'。或者,也可以使用特殊的带有显式上下文的函数生成'bind()'函数来确保“this”指向你需要的内容,用'var load = function(){this.iLoad =真正; } .bind(this)' – 2014-10-01 02:59:23

+0

现在确定关闭:)只有最后一部分有这个问题留下:)试过'readf:function(e){...}。bind(this)'see edit – 2014-10-01 03:07:44