This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Polymer Custom Elements: Data Binding

5:12 JavaScript lesson by

Polymer provides us a variety of data-binding configurations. In this lesson we'll look at configuring Two-way, One-way-down and One-way-up data binding.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Polymer provides us a variety of data-binding configurations. In this lesson we'll look at configuring Two-way, One-way-down and One-way-up data binding.

Avatar
Hack to the Future 1

i'm getting a JS warning in firefox that says: "no element found"
everything works fine, but wondering about that warning?

In reply to egghead.io

In this lesson, we're going to talk about data binding between a parent and a child element.

Here in the top, we're going to set up our parent element. I'm going to call this parent element. It's going to be a really simple element. We're just going to have a paragraph tag that says it's parent just so we can identify it in the browser. We're going to have an input with the type of text. We're going to go ahead and set our value equal to, and we're going to data bind, we'll call it parent data, and we're going to change that on the onInput event of our input.

Then below that, we're going to drop our child element. Into that we're going to pass an attribute of data equal to our parent data value. Here in our child element, going to set that up so this will be child element, we'll have a paragraph tag here with child. We'll also have an input type of text, our value here will be the data that's passed in. Again, we'll update that on the input event of our input.

We'll have another paragraph tag here with output. Then we'll simply output our data value. Now to use this in the parent element, we're going to need to HTML import that, so child element to HTML.

We can see this here as we type in the parent element. We can see that it is populating the children element input. Of course, it's updating our output. If I reload that, we can see I type here parent.

Now you'll notice when I type here child is only updating its own value. It's not reporting back to the parent. What we have here is one way data binding where we're passing down to the parent or from the parent to the child. But we're not binding that data back up to the parent.

To do that, we're going to go ahead and set up our properties. Of course, we've got this data value. Its type is going to be string. We're going to set a key here called notify. We're going to set that to true. This is a property that allows us to notify any other host element that this property has changed.

Now we'll try this and we'll type parent. We can see that it's populating or passing its data down to the child. Now when we type child, we are in fact passing that data back up to the parent. Now we've achieved two way data binding as opposed to one way, downward data binding.

If we wanted to enforce the one way data binding from our parent component down to our child component, we can use the one way data syntax. This means that even though the child element is prepared to two way data bind with its host, the parent element is saying, "No, we don't want to two way data bind."

Now we've reinforced that. Here if I type parent, we are passing that value down to the child. Here if I pass child, you can see that we have not passed that data back up to the parent. We've only updated our own value.

If we go ahead and set that back to two way data binding, another thing we can do is we can enforce the opposite one way data binding where it's from child to parent. Everything right now is set for two way data binding. What I'm going to do is set this attribute to read only, so therefore any value that's passed in from the parent will not change this.

However, we can change our own value, so I'm going to give this a default value of child. We'll save that. What you can see is that, despite the fact that the parent passed in its parent data value, which had no value, to the child, the child updated that value with the default value and ignored anything that was passed in by the parents and, in turn, updated the parent.

Another interesting thing that happens with the notify, is we get a notify function automatically created for us. Here in our child component, let's say we're going to do this in the ready state, we're going to add an event listener to data-change. Whatever our property name is and then -change, this event is firing any time that that value is changed.

We'll just have a callback function here, and we'll just log out, let's take in our event. What we'll do is we'll log out the event detail.value. This is going to be how we get to the value of the attribute that's been changed, so we'll save that.

Now up here as I type, we can see that the value is being logged out there. If I come down here and type we can see again that the value is being logged out. We've got this automatic event listener created for us, and obviously we can utilize that however we please.

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