gpt4 book ai didi

javascript - Polymer v1.0 - 数据绑定(bind)更改未传播

转载 作者:行者123 更新时间:2023-11-27 23:57:41 25 4
gpt4 key购买 nike

尝试使用 Polymer v1.0,目前正在加载 2 个数据集。第一组将设置 View 并重复我的自定义元素。第二组是异步加载的,加载后它将尝试通过路由 ID 进行匹配(如果可用)。匹配时,它将自身作为对象插入到第一个对象中。

当我这样做时, View 不会更新,但如果我执行 console.log 并跟踪它,数据就在那里。

我发现如果我要清除第一个对象,使用异步并稍后将其设置回来, View 会更新,但这会导致我的显示暂时空白。我怎样才能强制它用更新的内容重新绘制?

第一个设置项目的 View 。

[ 
{
"anchors":[
{"anchorid":1,
"routes":[
{"name":"route 1", "routeid":1 },
{"name":"route 2", "routeid":2 },
{"name":"route 3", "routeid":3 }
]
},
{"anchorid":2,
"routes":[
{"name":"route 4", "routeid":4 },
{"name":"route 5", "routeid":5 },
{"name":"route 6", "routeid":6 }
]
}
]
},
{
"anchors":[
{"anchorid":3,
"routes":[
{"name":"route 7", "routeid":7 },
{"name":"route 8", "routeid":8 },
{"name":"route 9", "routeid":9 }
]
},
{"anchorid":4,
"routes":[
{"name":"route 10", "routeid":10 },
{"name":"route 11", "routeid":11 },
{"name":"route 12", "routeid":12 }
]
}
]
}
]

第二个数据集

 [
{"routeid":3, "status":2},
{"routeid":5, "status":3},
{"routeid":1, "status":1}
]

我的元素代码

    <dom-module id="anchor-listdebug">
<template>
<h1>Anchor list</h1>
<template is="dom-repeat" items="{{data}}">
<h2>Anchor Group </h2>
<template is="dom-repeat" items="{{item.anchors}}" as="anchordata">
<anchor-item anchor="{{anchordata}}"></anchor-item>

</template>
</template>
</template>

<script>
Polymer ({
is:"anchor-listdebug",
ready : function () {
//data is loaded here via iron-ajax
this.data = data;
//data2 is loaded on data response
this.data2 = data2;

// emulate 2nd set of data loading, if this.processData is called immediately, content updates fine.
this.async (this.processData, 1000);
},
processData: function () {
this.data.forEach (function (element) {
element.anchors.forEach (function (anchorElement){
anchorElement.routes.forEach (function (routeElement){
var myID = routeElement.routeid;
var data = this.data2.filter(function (record){
return record.routeid == myID;
});
if (data.length >0) {
data = data[0];
routeElement.data = data;
}
});
});
});
}
});
</script>
</dom-module>

<dom-module id="anchor-item">
<template>
<h2>Anchor ID: {{anchor.anchorid}} </h2>
<template is="dom-repeat" items="{{anchor.routes}}" as="routedata">

<route-item route="{{routedata}}"></route-item>
</template>

</template>

<script>
Polymer ({
is:"anchor-item",
properties: {
anchor:{ type:Object, notify:true}
}

});
</script>
</dom-module>

<dom-module id="route-item">
<template>
<h3>Route id: <span>{{route.routeid}}</span></h3>
<h3>Route name: <span>{{route.name}}</span></h3>
<h3>Route status: <span>{{route.data.status}}</span></h3>

</template>

<script>
Polymer ({
is:"route-item",
properties: {
route:{ type:Object, notify:true}
}

});
</script>
</dom-module>

最佳答案

确保使用 set API 来操作对象 ( docs )。而不是this.data.anchors = someValue使用this.set("data.anchors", someValue)

关于javascript - Polymer v1.0 - 数据绑定(bind)更改未传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32111046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com