2015-10-13 69 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 
    } 
});