简体   繁体   English

将数据“推入”聚合物元素的最有效方法

[英]Most efficient way of “pushing” data to Polymer elements

I have a situation in which I get data over a web socket, and performance is important. 我遇到的情况是我通过Web套接字获取数据,而性能很重要。 From the docs I understand that there are various ways of "pushing" the data I'm receiving to my Polymer elements, but I'm curious which one will be most efficient. 从文档中我了解到,可以采用多种方式将接收到的数据“推送”到我的Polymer元素,但是我很好奇哪种方法最有效。 So far I've created an element that can be included in a template, where the parent element will observe any changes in the data property and react accordingly. 到目前为止,我已经创建了可以包含在模板中的元素,其中父元素将观察data属性中的任何更改并做出相应的反应。 I've also been experimenting with using a Behavior to accomplish the same thing, though instead of needing to include a "data-element" in its template, it could just observe its own data property. 我也一直在尝试使用行为来完成相同的事情,尽管它不必在模板中包含"data-element" ,而只需观察自己的数据属性即可。 I realize I could also use something like iron-signals to "push" the data via an event. 我意识到我还可以使用诸如iron-signals类的东西通过事件“推送”数据。

I'm not sure any of these methods are very efficient, since most of the time the changes to the "data" object will only apply to a small subset of all the observers. 我不确定这些方法是否非常有效,因为在大多数情况下,对“数据”对象的更改将仅适用于所有观察者的一小部分。 Another possible solution would be to "observe" a dynamic path, so like data.pathx instead of data.* , which would drastically reduce the number of times the observer callback gets fired, but I haven't come across anything that leads me to think that's possible, since each of my elements won't know if it should observe pathx or pathz until creation. 另一种可能的解决方案是“观察”动态路径,例如data.pathx而不是data.* ,这将大大减少观察者回调被触发的次数,但是我没有遇到任何导致我转向认为这是可能的,因为我的每个元素在创建之前都不知道应该遵守pathx还是pathz

Like I said, performance is vital, and I feel there is way too much inefficiency if I have a small to medium sized dom-repeat of elements each observing a large data object of another element or individually holding a copy of that data on their own (like I assume a behavior would accomplish?). 就像我说的那样,性能至关重要,如果我有一个小型到中型的dom-repeat元素,每个元素都观察另一个元素的大data对象,或者自己单独持有该数据的副本,那么我会觉得效率很低(例如我认为某种行为会完成?)。

I've looked at iron-meta , but I haven't been able to successfully data-bind to it, and from what I can tell from the docs, this data needs to be queried, whereas I need to be notified of changes. 我已经看过iron-meta ,但是我无法成功将数据绑定到它,并且从我从文档中可以得知,需要查询此数据,而需要将更改通知我。

Polymer doesn't really "observe" changes in elements. Polymer并不真正“观察”元素的变化。 It just sets a setter for each property, and when it's called the UI is updated. 它只是为每个属性设置一个setter,并在调用时更新UI。 So a dom-repeat template will not observe any change inside an object bound to it. 因此,dom-repeat模板将不会观察到绑定到它的对象内部的任何变化。

What could impact performance is unnecessary DOM manipulation, so if just a small subset of the data changes, re assigning all the array to the property is not ideal, and you should use notifyPath with just the sub property path and value that changed. 可能会影响性能的是不必要的DOM操作,因此,如果仅一小部分数据更改,则不理想的是将所有数组重新分配给该属性,那么您应该仅将notifyPath与更改的子属性路径和值一起使用。 Polymer will only update the DOM nodes affected. Polymer只会更新受影响的DOM节点。

If you have a way of knowing what sub properties changed in your data then you could obtain the object paths that have changed and call notifyPath for each of those and only a small number of DOM nodes will be changed. 如果您有办法知道数据中哪些子属性发生了更改,那么您可以获取已更改的对象路径,并为每个路径调用notifyPath,只有少量的DOM节点将被更改。

Additional note: If the number of elements in your array change, (added/removed) you should use the Polymer base array manipulation methods to update the property of your Polymer element, so it will change the DOM efficiently. 附加说明:如果更改(添加/删除)了数组中元素的数量,则应使用Polymer基础数组操作方法来更新Polymer元素的属性,这样它将有效地更改DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM