Hacker News new | ask | show | jobs
by ebidel 4779 days ago
Data binding happens at the property level. For example, you can remove the id/for attributs from those elements and things still work correctly:

http://jsbin.com/ecejiy/2/edit

2 comments

Thanks for the example!

Does this mean that it is the value="{{age}}" attribute that is causing the binding?

What happens if I want to do something like: <input value="{{firstName}} {{lastName}}"></input>?

Correct. In that example, the value attribute is bound to the element's age property. Basically, anything in {{}}.

> <input value="{{firstName}} {{lastName}}">

This will work as you expect (http://jsbin.com/ecejiy/6/edit). Plus, if either of those property values change, the input.value will be updated accordingly.

By the way, you can also add/define your own syntax for MDV bindings: https://github.com/Polymer/mdv/blob/master/docs/syntax.md

I just tweaked the GP's example to used <input value="{{name}} ({{age}})"></input>, which rendered as expected. The field wasn't bound to the age or name properties though, which makes sense.

In practice, this almost seems like "sugar" that automatically binds an input to a value if it's value contains ONLY the property.

To test out the binding sugar, I tried using this input: <input value=" {{name}}"></input> (note the space before {{name}}), which did not bind {{name}} to the input.

I'm still of the opinion that the binding should be more explicit.

Edit: typo fix

This is good feedback. We wanted to start with an easy mode, so people could on ramp quickly. In this case, you just use {{ }} for various kinds of binding and Polymer will (try to) do the right thing. I suspect we will need an alternative syntax for folks like yourself that want an explicit, engineering mode.
If anyone is interested, I hacked together a proof-of-concept framework with a different take on the {{ }} style binding: https://news.ycombinator.com/item?id=5741708
Oh God, I'm having more ASP.Net WebForms flashbacks. Databinding is one of those things MS never really got right despite having something like a thousand kicks at that can.