Your First Model Driven View with Dart

Packed inside the Dart Web UI project is inspired by Model Driven Views (also known as MDV). While Web Components provides encapsulation and reusability, it doesn't say much about linking your data and models with your views. MDV is a set of techniques to help you bind your data to your views, and just like Web Components, you can use Dart with MDV-esque behavior today.

Everything you are about to read works across modern browsers, because Dart compiles to JavaScript.

The binding.

Intro

The MDV behavior of Dart Web UI (hereafter referred to as Web UI) helps you bind your data and models to the view in a declarative manner. Binding usually means "when the data changes in one location, change it in another." Typically, web frameworks can wire together the necessary callbacks to keep the view (any text or input fields) in sync with the models (the Dart objects that contain the state of the application).

Setup

Remember, you'll need a pubspec.yaml that included the web_components package:

 name: App  
 description: A sample application  
 dependencies:  
  web_ui: any  

If you're new to Dart Web Components, you might want to read my Your First Web Component with Dart post, or the Dart Web Components article. Just like Dart Web Components, for MDV to work you need to get the web_components package, which contains the dwc compiler. The compiler is what converts the MVC and WC code into vanilla Dart and HTML. You can use dart2js to convert the Dart into JavaScript and run these samples across all major browsers.

Sample code

Let's look at a simple example that takes some data from Dart and displays it on a web page. We also add a button that, when clicked, updates the data which in turn updates the data on the page. Binding in action!

Here is the HTML:

 <body>  
  <h1>Hello MDV</h1>  
  <p>MDV is {{ superlative }}</p>  
  <button id="change-it" on-click="changeIt()">Change</button>  
  <script type="application/dart" src="HelloWorld.dart"></script>  
  <script src="dart.js"></script>  
 </body>  

Note the {{ superlative }} is a placeholder for data, to be populated by the MDV code. The name superlative is the name of a variable in Dart code.

The on-click="changeIt()" is an attribute that tells MDV to run the changeIt method whenever the button is clicked. The name changeIt is the name of a top-level function in Dart code.

Here is the Dart code:

 import 'dart:math';  
 String superlative;  
 List<String> alternatives = const ['wicked cool', 'sweet', 'fantastic'];  
 Random random;  
 main() {  
  superlative = 'awesome';  
  random = new Random();
 }  
 changeIt() => superlative = alternatives[random.nextInt(alternatives.length)];  

Note the top-level variable named superlative, which is initially set to 'awesome' in main. When the page is first displayed, you will see:


(If you can't see the embedded demo above, you can try the live demo.)

How cool is that?! Here we see simple data binding in action.

When the button is clicked, the changeIt function is run. A random alternative is chosen and assigned to the variable superlative. After the button is clicked, the view is updated.

The MDV code takes care of all the bindings and updating. The Dart code doesn't contain any code or logic to update the view when a value changed.

There's a lot more to Dart's support for MDV, which I'll cover in future blog posts. You can see this and more code at https://github.com/sethladd/dart-web-components-tests  The work on Dart Web Components and MDV is just beginning, so please try it out and send feedback. Thanks!
1 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
  • 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
  • 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