Polymer v1.0 - 数据绑定更改不传播
Polymer v1.0 - Data Binding changes not propagating
我正在尝试 Polymer v1.0,目前正在加载 2 个数据集。第一组将设置视图并重复我的自定义元素。第二组是异步加载的,加载后它将尝试通过 routeid 匹配(如果可用)。匹配时,它将自己作为对象插入到第一个对象中。
当我执行此操作时,视图不会更新,但如果我执行 console.log 并跟踪它,数据就在那里。
我发现如果我要清除第一个对象,使用异步并稍后将其设置回来,视图会更新,但这会导致我的显示有一段时间是空白的。
我怎样才能强制它用更新后的内容重绘?
第一个设置项目的视图。
[
{
"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>
确保使用集合 API 来操作对象 (docs)。使用 this.set("data.anchors", someValue)
而不是 this.data.anchors = someValue
。
首先,如前所述,您需要使用 this.set("data.anchors", someValue)
。您还需要更改模板绑定,否则生成的通知将被忽略。您不必使用 anchor="{{anchordata}}"
,而是必须使用 anchor="{{anchordata.*}}"
来侦听所有记录更改,或者如果您希望更改仅响应数据中的一个特定键,则必须使用 anchor="{{anchordata.SPECIFIC_KEY}}"
。
更新了我的 forEach 循环以包含用于构造 set() 函数所用路径的索引。
<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;
this.async (this.processData, 1000);
//this.processData();
},
processData: function () {
var scope = this;
this.data.forEach (function (element, index1) {
element.anchors.forEach (function (anchorElement, index2){
anchorElement.routes.forEach (function (routeElement, index3){
var myID = routeElement.routeid;
var data = this.data2.filter(function (record){
return record.routeid == myID;
});
if (data.length >0) {
data = data[0];
var tpath = "data."+index1+".anchors."+index2+".routes."+index3+".data";
scope.set(tpath, data);
}
});
});
});
}
});
</script>
我正在尝试 Polymer v1.0,目前正在加载 2 个数据集。第一组将设置视图并重复我的自定义元素。第二组是异步加载的,加载后它将尝试通过 routeid 匹配(如果可用)。匹配时,它将自己作为对象插入到第一个对象中。
当我执行此操作时,视图不会更新,但如果我执行 console.log 并跟踪它,数据就在那里。
我发现如果我要清除第一个对象,使用异步并稍后将其设置回来,视图会更新,但这会导致我的显示有一段时间是空白的。 我怎样才能强制它用更新后的内容重绘?
第一个设置项目的视图。
[
{
"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>
确保使用集合 API 来操作对象 (docs)。使用 this.set("data.anchors", someValue)
而不是 this.data.anchors = someValue
。
首先,如前所述,您需要使用 this.set("data.anchors", someValue)
。您还需要更改模板绑定,否则生成的通知将被忽略。您不必使用 anchor="{{anchordata}}"
,而是必须使用 anchor="{{anchordata.*}}"
来侦听所有记录更改,或者如果您希望更改仅响应数据中的一个特定键,则必须使用 anchor="{{anchordata.SPECIFIC_KEY}}"
。
更新了我的 forEach 循环以包含用于构造 set() 函数所用路径的索引。
<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;
this.async (this.processData, 1000);
//this.processData();
},
processData: function () {
var scope = this;
this.data.forEach (function (element, index1) {
element.anchors.forEach (function (anchorElement, index2){
anchorElement.routes.forEach (function (routeElement, index3){
var myID = routeElement.routeid;
var data = this.data2.filter(function (record){
return record.routeid == myID;
});
if (data.length >0) {
data = data[0];
var tpath = "data."+index1+".anchors."+index2+".routes."+index3+".data";
scope.set(tpath, data);
}
});
});
});
}
});
</script>