0

使用观察者我有一个简单的火力点查询:聚合物模块

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

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

回答

1

默认情况下,此查询将来自/用户的节点实时更新到数组“患者”。所以你可以在不写任何观察者等的情况下立即使用它,例如在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