0

I have a simple firebase-query:

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

Is it possible create a callback to receive in real time the update of the users node? I read about observer but I don't understand how I can use it in my module.

KENdi
  • 7,576
  • 2
  • 16
  • 31
mary
  • 335
  • 1
  • 3
  • 11

1 Answers1

1

with this query by default the nodes from /users are updated in realtime to the array "patients". so you can use this immediatly without writing any observer or so on e.g. in a dom-repeat element. this element is updated when more nodes come in the query. anyway if you want to observe this array for changes ( and modify the data afterwards ) you need to set a observer in the properties block like 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);

}

Kind regards

chwzrlee

Chwzr Lee
  • 21
  • 5