我的问题是,我的聚合物组件似乎没有数据绑定到我的行为中定义的变量。想知道这是否得不到支持,或者我有缺陷的想法是什么。聚合物 - 数据绑定到行为变量
我已经分享了我的行为变量作为一种全局状态的使用行为的人:
<script>
var StackOverflowBehavior = {
properties: {
show: {
type: Boolean,
value: false,
notify: true
}
},
attached: function() {
$.ajax({
type: 'GET',
url: 'http://myRestApiURL',
success: this.onRequestSucceed,
error: this.onRequestFailed
});
},
onRequestSucceed: function(response) {
this.show = true; // i hardcoded this at the moment to make sure it changes. console logging shows that the variable is getting changed. also tried changing this to this.show
}
现在我勾了我的聚合物组分的行为:
<template is="dom-if" if="{{show}}">
<!-- SOME ELEMENTS HERE -->
</template>
Polymer({
is: "my-polymer-component",
behaviors: [
StackOverflowBehavior
],
的问题在于最初绑定到show变量的模板绑定到了初始的StackOverflowBehavior.show,所以模板不可见,但是当show变为true时,模板不会出现。同样,当我在StackOverflowBehavior中初始化show = true时,它开始可见,但是当我设置show = false时,模板不会像我所期望的那样消失。
我做了一个解决方法,即使用铁信号来发布show变量。然后,每个聚合物组件都有一个本地显示变量,它在接收信号时设置为新的显示变量。每个聚合物组分的模板都绑定到它的局部变量。这个工作。但是,当绑定到行为中的共享变量时,它不起作用。
编辑:好像如果我更改从附加函数更新变量,数据绑定到共享全局行为的作品,但如果我从用户定义的函数更改变量,它不会。从具有数据绑定模板的聚合物组件中,我可以在ready()方法内调用console.log(show),并打印出正确的值。
非常感谢帮助。
这个JS Bin与您的代码类似吗? http://jsbin.com/totoma/edit?html,js,output –
@TomaszPluskiewicz它是类似的,但行为在ready()中有一些ajax调用,处理回调的方法是用户定义的函数行为。 – jbu
更改'this.show'而不是'show'的值,并确保您的'attach'和'onRequestSucceed'属于'properties'对象之外。 – a1626