0

Ich habe eine einfache Feuerbasis-Abfrage:Verwendung Beobachter in Polymer-Modul

<firebase-query 
    id="query" 
    app-name="app_name" 
    path="/users" 
    data="{{patients}}"> 
</firebase-query> 

Ist es möglich, einen Rückruf erstellen und in Echtzeit die Aktualisierung der Benutzer Knoten erhalten? Ich lese über Beobachter, aber ich verstehe nicht, wie ich es in meinem Modul verwenden kann.

Antwort

1

Mit dieser Abfrage werden standardmäßig die Knoten von/users in Echtzeit auf das Array "patients" aktualisiert. Sie können dies sofort verwenden, ohne einen Beobachter oder so zu schreiben, z. in einem dom-repeat-Element. Dieses Element wird aktualisiert, wenn mehr Knoten in die Abfrage kommen. sowieso wenn Sie möchten, um dieses Array für Änderungen beobachten (und die Daten ändern danach) benötigen Sie einen Beobachter in den Eigenschaften einzustellen blockieren etwa so:

static get properties() { 
    return { 
    patients: { 
     // use only one of these types:Boolean | Number | String | Array | Object, 
     // in your case i think its an Array 
     type: Array 
     // For an Array or Object, you must return it from a function 
     // (otherwise the array will be defined on the prototype 
     // and not the instance): 
     value: function() { return [] }, 
     // other elements need to get informed if the value changes? then add this      
     // line: 
     notify: true, 
     // now set the observer 
     observer: '_patientsChanged', 
    }, 
    } 
} 

// and the observer function itself: 

_patientsChanged(newVal, oldVal){ 
    // do whatever you want to do e.g. Log the change of the Variable in the  
    // Console: 

    console.log('Variable patients changed: It had this value: '); 
    console.log(oldVal); 
    console.log('And now it has this Value:'); 
    console.log(newVal); 

} 

Mit freundlichen Grüßen

chwzrlee