我希望构建一个数据类型的行为来监听属性更新。 听取儿童财产变化的最佳方式是什么?
function do_something_when_property_updates(property_name) {
return {
// How to listen to property?
properties: {
property_name: {
// This behavior doesn't (and shouldn't) own the property
observer: foo
}
},
foo: function(value) {
// Update handler
}
};
}
Polymer({
is:'test-element',
properties: {
bar: {
type: Object
}
},
behaviors: [do_something_when_property_updates('bar')]
});
答案 0 :(得分:1)
使用observers:[]
属性。 See the docs
function do_something_when_property_updates(property_name) {
return {
observers: [
// Note .* will observe any changes to this object or sub-object
'foo(' + property_name + '.*)'
],
foo: function(value) {
// Update handler
}
};
}
Polymer({
is:'test-element',
properties: {
bar: {
type: Object
}
},
behaviors: [do_something_when_property_updates('bar')],
ready: function() {
this.set('bar.a', {}); // foo() is invoked
this.set('bar.a.b', true); // foo() is invoked
}
});