

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





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

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!

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.









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)];

Here is the Dart code:

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.



