Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic update of dom-repeat templates by changing the underlying array

Is there a Polymer dom api method or other ways to automatically update the repeated template with additional elements, should the underlying array gets updated?

I have a dynamic array that gets updated through ajax calls repeatedly. This array keeps changing its length (elements get incrementally appended or removed based on the ajax response). The array is reflected in the document through <template is="dom-repeat">. Is there a way to automatically update the dom when the underlying array gets updated?

At present, the only ways I can see are

  1. Identifying the incremental updates and manually append or delete the nodes.
  2. Delete the entire repeat template container and restamp a new template. I do not know of anyway to stamp a new template dynamically either. Is this possible? One way I can think of achieving this is having another custom element that does repeat template stamping and whenever there is an array update, delete the current element, append a new element by passing the updated array to the element.

By using 2, I will not be able to use the transition effects of the list automatically updating in the view. I want to be able to use these effects.

like image 836
Aravind Avatar asked Jun 11 '15 20:06

Aravind


People also ask

What is Dom repeat?

The <dom-repeat> element will automatically stamp and binds one instance of template content to each object in a user-provided array. dom-repeat accepts an items property, and one instance of the template is stamped for each item into the DOM at the location of the dom-repeat element.

Which helper element will you use to improve the memory footprint of large and coplex sites?

Conditional templates introduce some overhead, so they shouldn't be used for small UI elements that could be easily shown and hidden using CSS. Instead, use conditional templates to improve loading time or reduce your page's memory footprint.

What is a dom if?

Element <dom-if> (DomIf) Custom element that conditionally stamps and hides or removes template content based on a boolean flag.


1 Answers

If you update the array with this.push('myarray', {item}); then that will trigger Polymer's binding observers. More info is in the documentation at https://www.polymer-project.org/1.0/docs/devguide/properties#array-mutation

like image 171
Zikes Avatar answered Oct 04 '22 00:10

Zikes