This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Polymer Custom Elements: dom-repeat

2:49 JavaScript lesson by

In this lesson we'll use the "dom-repeat" helper method in our custom element to iterate over a collection and use the built in model instantiation to update items in our collection.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson we'll use the "dom-repeat" helper method in our custom element to iterate over a collection and use the built in model instantiation to update items in our collection.

Avatar
Ravindran

Excellent introduction highlighting major features of polymer. Looking forward for other topics in polymer

Avatar
Devin

Good intro. Gave me a clear sense of the mechanics.

In this lesson, we are going to talk about iterating over a collection inside of our element template using DOM repeat.

Here in the ready method of my cart list, I'm assigning a variable of this.food to this array right here, where we have a name and a quantity for pizza, taco, and burger.

To do this, we're going to add another template inside of our template. Onto that, we're going to tack on an is attribute of dom-repeat. This is going to set us up to iterate over that collection. To get that collection in there, I'm going to have a key here called items, and into that, I'm going to pass in foods.

Here in center template, I'm going to set up a div to hold everything. This first time around, we're going to have a span.

Now, to get at our items, I'm going to have item and I'm going to say item.name. Let's see, we'll also tack on our quantity here, so item.quantity. Save that, and over here on the right, we can see that we are, in fact, iterating over those items.

We can also get to our index, so here I'm going to say hashtag and I'm going to pass in index, which is a value that's automatically passed through during the iteration. We can see we have our index of zero, one, and two.

Lastly, an interesting thing that happens here is we actually get each of these items converted over to a model that's available for any custom events.

I'm going to create a button here and I'll just have it enter HTML of a plus symbol, and I'm going to say on click of that. It's going to equal add, which would be a custom event, so I add that right down here. So I add, it'll be a function. It's going to take in an event.

We can get to the model that the DOM repeat has created by getting to e.model. We can set a new value by calling it set method. I'm going to set the item.quantity to e.model.item.quantity plus one. We'll increase that value, so let's try that out.

Each time I click on this, it's increasing the quantity of that item. We can actually get a look at the fact that it's actually updating, or this.foods, collection by...Let's see, we're going to log out this.foods. I'll just grab our first one, so that'll be the pizza item. We'll log out its quantity right after the add method has been called.

When I click on this, you can see that the value is actually being updated as we add new items to our cart.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?