Polymer v1.0 - 数据绑定更改未传播

时间:2015-08-20 06:32:19

标签: javascript polymer polymer-1.0 2-way-object-databinding

尝试使用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>

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

首先,您需要使用前面所述的this.set("data.anchors", someValue)。您还需要更改模板绑定,否则将忽略生成的通知。您不必使用anchor="{{anchordata}}",而是必须使用anchor="{{anchordata.*}}"来收听所有记录更改,或者anchor="{{anchordata.SPECIFIC_KEY}}",如果您希望更改只响应数据中的一个特定键。

答案 2 :(得分:0)

更新了我的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>