2016-08-19 57 views
1

我試圖集成Firebaseapp-indexeddb-mirror元素來緩存用戶數據並在他離線時傳遞它。Firebase和應用程序indexeddb鏡像集成不起作用

我停止了一個元素,該元素應該列出我的Firebase數據庫中所有用戶的名稱,即使在我離線時也是如此。

<link rel="import" href="../bower_components/polymer/polymer.html"> 
<link rel="import" href="../bower_components/polymerfire/polymerfire.html"> 
<link rel="import" href="../bower_components/app-storage/app-indexeddb-mirror/app-indexeddb-mirror.html"> 

<dom-module id="firebase-test"> 
    <template><!--{{{--> 
     <style>/*{{{*/ 
    </style><!--}}}--> 

     <firebase-app 
      name="app" 
      auth-domain="my-app.firebaseapp.com" 
      database-url="https://my-app.firebaseio.com" 
      api-key="my-key"> 
     </firebase-app> 

     <app-indexeddb-mirror 
      key="teste" 
      data="{{firebaseData}}" 
      persisted-data="{{data}}" 
     ></app-indexeddb-mirror> 

     <firebase-query 
      id="query" 
      app-name="app" 
      path="/user" 
      data="{{firebaseData}}"> 
     </firebase-query> 

     <template is="dom-repeat" items="{{data}}"> 
      <h1>{{item.name}}</h1> 
     </template > 

    </template><!--}}}--> 

    <script>//{{{ 
    Polymer({ 
     is: 'firebase-test', 
    }); 
    </script><!--}}}--> 
</dom-module> 

當我運行應用程序離線我得到的App IndexedDB Client connecting..在控制檯中,是什麼讓我懷疑app-network-status-behavior未能發現當我下線。

回答

0

更改app-network-status-behavior工作。我正在使用Firebase系統來驗證連接狀態。

<!-- 
@license 
Copyright (c) 2016 The Polymer Project Authors. All rights reserved. 
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt 
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt 
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt 
Code distributed by Google as part of the polymer project is also 
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt 
--> 
<link rel="import" href="../polymer/polymer.html"> 
<script> 
    (function() { 
    'use strict'; 

    var networkStatusSubscribers = []; 

    firebase.database().ref('.info/connected') 
    .on('value',function(snap){ 
     for (var i = 0; i < networkStatusSubscribers.length; ++i) { 
     networkStatusSubscribers[i].refreshNetworkStatus(snap.val()) 
     } 
    }) 

    /** 
    * `Polymer.appNetworkStatusBehavior` tracks the status of whether the browser 
    * is online or offline. True if the browser is online, and false if the browser is 
    * offline matching the HTML browser state spec. 
    * 
    * @polymerBehavior 
    */ 
    Polymer.AppNetworkStatusBehavior = { 
     properties: { 
     /** 
     * True if the browser is online, and false if the browser is offline 
     * matching the HTML browser state spec. 
     * 
     * @type {Boolean} 
     */ 
     online: { 
      type: Boolean, 
      readOnly: true, 
      notify: true, 
      value: function() { 
      firebase.database().ref('.info/connected') 
      .once('value',function(snap){ 
       return snap.val() 
      }) 
      } 
     } 
     }, 

     attached: function() { 
     networkStatusSubscribers.push(this); 
     this.refreshNetworkStatus(); 
     }, 

     detached: function() { 
     var index = networkStatusSubscribers.indexOf(this); 
     if (index < 0) { 
      return; 
     } 
     networkStatusSubscribers.splice(index, 1); 
     }, 

     /** 
     * Updates the `online` property to reflect the browser connection status. 
     */ 
     refreshNetworkStatus: function(netStatus) { 
     this._setOnline(netStatus); 
     } 
    }; 
    })(); 
</script>