在聚合物模块中使用观察者



我有一个简单的firebase-Query:

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

是否可能创建一个回调以实时接收用户节点的更新?我阅读了有关观察者的信息,但我不明白如何在模块中使用它。

使用此查询,默认情况下,来自/用户的节点实时更新为"患者"数组。因此,您可以立即使用此信息,而无需编写任何观察者左右,例如在dom-repeat元素中。当更多的节点出现在查询中时,此元素会更新。反正如果要观察此数组以进行更改(然后修改数据(,则需要在类似的属性块中设置一个观察者:

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);
}

善意

chwzrlee

最新更新