Wednesday, November 30, 2011

Started Learning Dart



I have started learning Dart. First, I read Why Dart Excites Me. Second, I installed Dart Editor. Third, I developed a small, model-driven web application called WebLink.

The WebLink application has a model with only one concept called Url (Url.dart). The Url class has two properties: link and description. A url is a part of the model.


<pre class="brush:java">
class Url {
 LinkModel linkModel;I am Dzenan.
 String link;
 String description;
 
 Url(LinkModel this.linkModel) {
 }
}
</pre>






A collection of web links is represented by the Urls class (Urls.dart).



class Urls {
 LinkModel linkModel;
 
 List<Url> list;
 
 Urls(LinkModel this.linkModel) {
   list = new List();
 }
 
 void add(Url url) {
   list.add(url);
 }
 
 Iterator<Url> iterator() {
   return list.iterator();
 }
}


The model (LinkModel.dart) has one entry point that is a collection of urls.



class LinkModel {
 Urls urls;



 LinkModel() {
   urls = new Urls(this);
 }
 
 void createUrls() {
   Url modelibra = new Url(this);
   modelibra.link = "http://www.modelibra.org/";
   modelibra.description = "Domain model framework";
   urls.add(modelibra);
   
   Url dart = new Url(this);
   dart.link = "http://www.dartlang.org/";
   dart.description =
"Dart is a programming language for creating web applications.";
   urls.add(dart);
 }
}


The web application (WebLink.dart) creates the empty model, fills the model with two urls and displays them in the web page.



#import('dart:html');
#source('Url.dart');
#source('Urls.dart');
#source('LinkModel.dart');



class WebLink {
 LinkModel linkModel;



 WebLink() {
   linkModel = new LinkModel();
 }
 
 void displayUrls() {
   String r = "";
   for (Url url in linkModel.urls) {
     r = r + url.link + "<br/>" + url.description + "<br/><br/>";
   }
   write(r);
 }
 
 void write(String message) {
   document.query('#status').innerHTML = message;
 }
}



void main() {
 WebLink webApp = new WebLink();
 webApp.linkModel.createUrls();
 webApp.displayUrls();
}


The web page (WebLink.html) has a title (html) and a list of web links with their descriptions (js).



<html>
 <head>
   <title>WebLink</title>
 </head>
 <body>
   <h1>Web Links</h1>
   <h2 id="status">dart is not running</h2>
   <script type="text/javascript" src="WebLink.dart.app.js"></script>
 </body>
</html>


The application is created and run in the Dart Editor.


The following is the content of the web page in Chrome:

Web Links

http://www.modelibra.org/
Domain model framework

http://www.dartlang.org/
Dart is a progra
mming language for creating web applications.

No comments:

Post a Comment