聚合物:强制设置 属性 不会改变
Polymer: Imperatively set property doesn't changes
我现在被困了两天了,我对即将描述的这个问题一头雾水。
我做了一个傻瓜:http://plnkr.co/edit/OrUiqcFfb70VFtO06sNT
所以,我有 <main-element>
- 这个元素包含两个子元素:<child-element>
和 <sub-main>
。这两个元素都绑定在 <main-element>
中设置的 owner
对象上。像这样:
<child-element owner="{{owner}}"></child-element>
现在,<sub-main>
包含另一个元素 - <sub-child-element>
。
<template>
<sub-child-element id="sub"></sub-child-element>
</template>
但它强制绑定 owner
属性:
attached: function() {
this.$.sub.owner = this.owner;
}
您可能已经猜到 <sub-child-elements>
没有看到对 owner
对象所做的更改。我试图在整个地方洒上 this.set
和 this.notifyPath
但它没有帮助。
有什么方法可以实现吗?甚至可以做我想做的事吗?
您可以像这样在 sub-main
中观察 owner
。
observers: [
'ownerChanged(owner.*)'
],
每当它发生变化时,通知sub-child-element
。
ownerChanged: function(event){
this.$.sub.notifyPath(event.path, event.value);
}
这是执行此操作的 fork 你的 plunker。
更新
也可以观察具体的路径,比如owner.details.location
。请注意,处理程序的参数将是新值。对于 deep path observation(例如 owner.*
),处理程序接收更改记录作为参数。
因此,在您的示例中,您可以按如下方式进行。
observers: [
'ownerChanged(owner.details.location)'
],
ownerChanged: function(newValue){
this.$.sub.notifyPath("owner.details.location", newValue);
}
我现在被困了两天了,我对即将描述的这个问题一头雾水。
我做了一个傻瓜:http://plnkr.co/edit/OrUiqcFfb70VFtO06sNT
所以,我有 <main-element>
- 这个元素包含两个子元素:<child-element>
和 <sub-main>
。这两个元素都绑定在 <main-element>
中设置的 owner
对象上。像这样:
<child-element owner="{{owner}}"></child-element>
现在,<sub-main>
包含另一个元素 - <sub-child-element>
。
<template>
<sub-child-element id="sub"></sub-child-element>
</template>
但它强制绑定 owner
属性:
attached: function() {
this.$.sub.owner = this.owner;
}
您可能已经猜到 <sub-child-elements>
没有看到对 owner
对象所做的更改。我试图在整个地方洒上 this.set
和 this.notifyPath
但它没有帮助。
有什么方法可以实现吗?甚至可以做我想做的事吗?
您可以像这样在 sub-main
中观察 owner
。
observers: [
'ownerChanged(owner.*)'
],
每当它发生变化时,通知sub-child-element
。
ownerChanged: function(event){
this.$.sub.notifyPath(event.path, event.value);
}
这是执行此操作的 fork 你的 plunker。
更新
也可以观察具体的路径,比如owner.details.location
。请注意,处理程序的参数将是新值。对于 deep path observation(例如 owner.*
),处理程序接收更改记录作为参数。
因此,在您的示例中,您可以按如下方式进行。
observers: [
'ownerChanged(owner.details.location)'
],
ownerChanged: function(newValue){
this.$.sub.notifyPath("owner.details.location", newValue);
}