2015-10-13 72 views
0

我想建立一个数据类型的行为,听取属性更新。 倾听儿童财产变化的最佳方式是什么?聚合物行为听取儿童财产变化

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

回答

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