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.


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).


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

 name: App  
 description: A sample application  
  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:

  <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>  

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  The work on Dart Web Components and MDV is just beginning, so please try it out and send feedback. Thanks!

Popular posts from this blog

Lists and arrays in Dart

Converting Array to List in Scala

The 29 Healthiest Foods on the Planet