Your First Input Field Binding with Web Components and Dart

Updating a web page when the data changes is cool. Updating the data when the web page changes is even cooler. Read on to learn how to create a live data binding between form fields, data, and your web page with Web Components, Model Driven Views, and Dart.

Artful binding.
(You may want to read Your First Model Driven View with Dart, which contains more information on getting started. This post assumes you've read my previous posts on Web Components and Dart.)

What do I mean, binding?

In the context of user interfaces, data binding is the technique of keeping both data and the view in sync. If the data changes, update the view. If the view changes, update the data. It's the later scenario (view changing the data) that I discuss in this post.

The HTML

HTML views can update data via input fields. Input fields can be bound to data, both displaying the data and updating the data. Use the data-bind attribute on input fields such as text, textarea, and select.

 <p>MDV is {{ superlative }}</p>  
 <div>  
  <input type="text" bind-value="superlative" placeholder="Enter superlative">  
 </div>  

The above code sample shows two data bindings. The first, {{ superlative }}, we saw in my previous post. The second, data-bind, is how you declare a binding for an input field.

Specifically, bind-value="superlative" is saying "bind the superlative variable found in Dart code to the value attribute of this input field". The Dart Web Components document calls this "two-way binding", because the input field itself is bound in two ways: reading and writing the variable.

The Dart

In this simple example, the Dart code only declares the variable and defines the main method.

 String superlative = '';  
 main() { }  

One of the main benefits of Web Components is that everything is very declarative, so the need for glue code or explicit event handlers is diminished.

The results

Best to show this off live, so you can see the live data binding.


Notice how the display is updated as the user types in the input field. To make this happen, almost no code was created, and everything was simply declared in the HTML. Awesome!

Summary

Use the data-bind attribute on input fields to declare live "two-way" data binding. When the variable's value is changed, the input field changes. When the user updates the input field, the variable's value changes.

Read the Dart Web Components documentation or check out my other blog posts on Dart Web Components. The code from this post, and other examples of Dart Web Components, is available on Github.

Thanks!

(Binding image courtesy of http://www.flickr.com/photos/ruthbleakley/3098090721/)
Post a Comment

Popular posts from this blog

  • Sponsor:  Register today for  New Game, the conference for HTML5 game developers . Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.  Register now ! This is the second article in a Box2D series, following the Box2D Orientation article. The Box2DWeb port of Box2D contains a nice example to show off the basics of integrating physics simulations into your web app. This post will provide a walkthrough of the example, explaining the high level concepts and code. First, let's see the example in action. The code for the above is open source and available on GitHub. It was adapted from Box2DWeb's example . Animating Before we look at Box2D, it's important to understand how the above simulation is animated. You might think setInterval or setTimeout is
    Keep reading
  • In which I port a snazzy little JavaScript audio web app to Dart , discover a bug, and high-five type annotations. Here's what I learned. [As it says in the header of this blog, I'm a seasoned Dart developer. However, I certainly don't write Dart every day (I wish!). Don't interpret this post as "Hi, I'm new to Dart". Instead, interpret this post as "I'm applying what I've been documenting."] This post analyzes two versions of the same app, both the original (JavaScript) version and the Dart version. The original version is a proxy for any small JavaScript app, there's nothing particularly special about the original version, which is why it made for a good example. This post discusses the differences between the two implementations: file organization, dependencies and modules, shims, classes, type annotations, event handling, calling multiple methods, asynchronous programming, animation, and interop with JavaScript libraries. F
    Keep reading
  • Angular and Polymer, sitting in a DOM tree, B-i-n-d-i-n-g. First comes components, Then comes elements, Then comes the interop with the node dot bind. Angular , a super heroic MVC framework, and Polymer , polyfills and enhancements for custom elements built on top of Web Components, can live harmoniously in the same app. This post shows you how to connect Angular-controlled components to Polymer-controlled elements via data binding. And we do it all in Dart . Angular and Polymer I get asked "Should I use Angular or Polymer?" a lot. My answer is, "Yes". That is, both libraries have distinct strengths, and you can use both in the same app. Polymer excels at creating encapsulated custom elements. You can use those custom elements in any web app or web page, regardless if that app is built with Angular, Ember, etc. Angular excels at application engineering, with dependency injection, end-to-end testability, routing, and services. Here are som
    Keep reading