【问题标题】:Use observer in polymer module在聚合物模块中使用观察者
【发布时间】:2017-10-03 15:54:00
【问题描述】:

我有一个简单的 firebase 查询:

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

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

【问题讨论】:

    标签: javascript firebase firebase-realtime-database observers polymerfire


    【解决方案1】:

    默认情况下,使用此查询,来自 /users 的节点会实时更新为数组“患者”。因此您可以立即使用它而无需编写任何观察者等,例如在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 2014-10-10
      相关资源
      最近更新 更多