introduction to dart par yohan beschi

133
2013-04-10 Introduction to Dart 1 DART Yohan BESCHI – Java Developer @yohanbeschi +Yohan Beschi

Upload: soat

Post on 19-May-2015

708 views

Category:

Technology


1 download

DESCRIPTION

Part 1 - Introduction à Dart Dart s'inscrit dans la mouvance des frameworks JavaScript (Node.js, Backbone, AngularJS, etc,) et des langages compilés en JavaScript (CoffeeScript, TypeScipt, etc.) dans l'objectif de faciliter le développement Web. Les applications Web sont depuis quelques années le type d'application privilégié des développeurs. Néanmoins, les outils que nous possédions jusqu'à très récemment n'étaient absolument pas adaptés au monde professionnel. Dart offre la possibilité de développer, dans un langage structuré et objet, des applications clients et serveurs, et ceci en un temps record. Mais Dart n'est pas seulement un langage c'est aussi tout un écosystème au service de l'industrialisation. Au cours de cette présentation nous verrons : • comment Dart permet de développer des applications clientes aussi bien que serveurs, • quelques éléments du langage, • mais aussi les outils permettant un développement industrialisé et une productivité accrue Durée : 45min/1h Part 2 - Live Coding - Développer une application à page unique en Dart Bien que la version finale de Dart ne soit pas encore disponible (pour cela il faudra attendre cet été), il est utilisable dès aujourd'hui. Et c'est ce que nous verrons au cours de cette deuxième partie en nous appuyant sur une application simple. Durée : 45min/1h

TRANSCRIPT

Page 1: Introduction to dart par Yohan Beschi

2013-04-10 Introduction to Dart 1

DART

Yohan BESCHI – Java Developer

@yohanbeschi

+Yohan Beschi

Page 2: Introduction to dart par Yohan Beschi

2013-04-10 Introduction to Dart 2

Page 3: Introduction to dart par Yohan Beschi

Building UIs - Javascript ?

2013-04-10 Introduction to Dart 3

Page 4: Introduction to dart par Yohan Beschi

Building UIs - Java ?

2013-04-10 Introduction to Dart 4

Page 5: Introduction to dart par Yohan Beschi

Building UIs with Java - But how ?

2013-04-10 Introduction to Dart 5

Page 6: Introduction to dart par Yohan Beschi

Programmatic Components with GWT

2013-04-10 Introduction to Dart 6

CellTable<User> table = new CellTable<User>();

TextColumn<User> idColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.id;

}

};

TextColumn<User> firstNameColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.firstName;

}

};

TextColumn<User> lastNameColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.lastName;

}

};

TextColumn<User> ageColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.age;

}

};

idColumn.setSortable(true);

firstNameColumn.setSortable(true);

lastNameColumn.setSortable(true);

ageColumn.setSortable(true);

table.addColumn(idColumn, "ID");

table.addColumn(firstNameColumn, "First name");

table.addColumn(lastNameColumn, "Lats name");

table.addColumn(ageColumn, "Age");

ListDataProvider<User> dataProvider = new ListDataProvider<User>();

dataProvider.addDataDisplay(table);

List<User> list = dataProvider.getList();

for (User user : USERS) {

list.add(user);

}

ListHandler<User> columnSortHandler = new ListHandler<Tester.User>(list);

columnSortHandler.setComparator(idColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.id.compareTo(o2.id) : 1;

}

return -1;

}

});

columnSortHandler.setComparator(firstNameColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.firstName.compareTo(o2.firstName) : 1;

}

return -1;

}

});

columnSortHandler.setComparator(lasteNameColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.lasteName.compareTo(o2.lasteName) : 1;

}

return -1;

}

});

columnSortHandler.setComparator(ageColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.age.compareTo(o2.age) : 1;

}

return -1;

}

});

table.addColumnSortHandler(columnSortHandler);

table.getColumnSortList().push(firstNameColumn);

Page 7: Introduction to dart par Yohan Beschi

Programmatic Components with GWT

2013-04-10 Introduction to Dart 7

CellTable<User> table = new CellTable<User>();

TextColumn<User> idColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.id;

}

};

TextColumn<User> firstNameColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.firstName;

}

};

TextColumn<User> lastNameColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.lastName;

}

};

TextColumn<User> ageColumn = new TextColumn<User>() {

@Override

public String getValue(User user) {

return user.age;

}

};

idColumn.setSortable(true);

firstNameColumn.setSortable(true);

lastNameColumn.setSortable(true);

ageColumn.setSortable(true);

table.addColumn(idColumn, "ID");

table.addColumn(firstNameColumn, "First name");

table.addColumn(lastNameColumn, "Lats name");

table.addColumn(ageColumn, "Age");

ListDataProvider<User> dataProvider = new ListDataProvider<User>();

dataProvider.addDataDisplay(table);

List<User> list = dataProvider.getList();

for (User user : USERS) {

list.add(user);

}

ListHandler<User> columnSortHandler = new ListHandler<Tester.User>(list);

columnSortHandler.setComparator(idColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.id.compareTo(o2.id) : 1;

}

return -1;

}

});

columnSortHandler.setComparator(firstNameColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.firstName.compareTo(o2.firstName) : 1;

}

return -1;

}

});

columnSortHandler.setComparator(lasteNameColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.lasteName.compareTo(o2.lasteName) : 1;

}

return -1;

}

});

columnSortHandler.setComparator(ageColumn,

new Comparator<Tester.User>() {

public int compare(User o1, User o2) {

if (o1 == o2) {

return 0;

}

if (o1 != null) {

return (o2 != null) ? o1.age.compareTo(o2.age) : 1;

}

return -1;

}

});

table.addColumnSortHandler(columnSortHandler);

table.getColumnSortList().push(firstNameColumn);

Page 8: Introduction to dart par Yohan Beschi

The Dart Way

2013-04-10 Introduction to Dart 8

Table<User> table = new Table (sorting:true)

..addColumn('ID', new TextCell((User o) => o.id))

..addColumn('First name', new TextCell((User o) => o.firstName))

..addColumn('Last name', new TextCell((User o) => o.lastName))

..addColumn('Age', new TextCell((User o) => o.age))

..setData(objs);

Page 9: Introduction to dart par Yohan Beschi

The Dart Way

2013-04-10 Introduction to Dart 9

Table<User> table = new Table (sorting:true)

..addColumn('ID', new TextCell((User o) => o.id))

..addColumn('First name', new TextCell((User o) => o.firstName))

..addColumn('Last name', new TextCell((User o) => o.lastName))

..addColumn('Age', new TextCell((User o) => o.age))

..setData(objs);

6 lines

Page 10: Introduction to dart par Yohan Beschi

Dart is the winner

2013-04-10 Introduction to Dart 10

Page 11: Introduction to dart par Yohan Beschi

Once upon a time…

2013-04-10 Introduction to Dart 11

Page 12: Introduction to dart par Yohan Beschi

Programmer productivity

2013-04-10 Introduction to Dart 12

Page 13: Introduction to dart par Yohan Beschi

Application scalability

2013-04-10 Introduction to Dart 13

Page 14: Introduction to dart par Yohan Beschi

Raw execution speed

2013-04-10 Introduction to Dart 14

Page 15: Introduction to dart par Yohan Beschi

Startup performance

2013-04-10 Introduction to Dart 15

Page 16: Introduction to dart par Yohan Beschi

And here we are!

⦿Open Source (BSD)

⦿Structured

⦿Anti-Revolutionary

⦿Same goals as new Javascript frameworks

⦿The goal is to not break the web

2013-04-10 Introduction to Dart 16

Page 17: Introduction to dart par Yohan Beschi

Dart Ecosystem

2013-04-10 Introduction to Dart 17

Dart

Tools

VMs

Dart Editor

Dartium

dart2js

pub

dartdoc

Server

Browser

dart:io

dart:html

Page 18: Introduction to dart par Yohan Beschi

Virtual Machines

2013-04-10 Introduction to Dart 18

Page 19: Introduction to dart par Yohan Beschi

Dartium

2013-04-10 Introduction to Dart 19

Page 20: Introduction to dart par Yohan Beschi

DartEditor

2013-04-10 Introduction to Dart 20

Page 21: Introduction to dart par Yohan Beschi

Plugins

2013-04-10 Introduction to Dart 21

Page 22: Introduction to dart par Yohan Beschi

dart2js

2013-04-10 Introduction to Dart 22

Page 23: Introduction to dart par Yohan Beschi

dart2js

2013-04-10 Introduction to Dart 23

Page 24: Introduction to dart par Yohan Beschi

dart2js

⦿Target HTML5

⦿Tree Shaking

⦿Aggregation/Minification

⦿Optimization

2013-04-10 Introduction to Dart 24

Page 25: Introduction to dart par Yohan Beschi

pub

2013-04-10 Introduction to Dart 25

Page 26: Introduction to dart par Yohan Beschi

pub

2013-04-10 Introduction to Dart 26

name: pacifista_rocks

description: The best application in the whole world

version: 0.0.1

dependencies:

great_lib: any

pubspec.yaml

Page 27: Introduction to dart par Yohan Beschi

dartdoc

2013-04-10 Introduction to Dart 27

/// This is a single-line documentation comment.

/**

* This is a multi-line documentation comment.

* To generate the documentation:

* $ dartdoc <filename>

*/

void main() {

}

Page 28: Introduction to dart par Yohan Beschi

dartdoc

2013-04-10 Introduction to Dart 28

Page 29: Introduction to dart par Yohan Beschi

Dart – The Language

2013-04-10 Introduction to Dart 29

https://github.com/yohanbeschi/lang.dart

Page 30: Introduction to dart par Yohan Beschi

Entry Point

2013-04-10 Introduction to Dart 30

void main() {

}

Page 31: Introduction to dart par Yohan Beschi

Comments

2013-04-10 Introduction to Dart 31

void main() {

// This is a single-line comment.

/*

* This is a

* multi-line

* comment.

*/

}

Page 32: Introduction to dart par Yohan Beschi

Types - Boolean

2013-04-10 Introduction to Dart 32

var boolean1 = true;

var boolean2 = false;

bool boolean3 = true;

bool boolean4 = false;

Page 33: Introduction to dart par Yohan Beschi

Types - String

2013-04-10 Introduction to Dart 33

String singleQuotes = 'Single quotes.';

String doubleQuotes = "Double quotes.";

// String interpolation

print('Hello $singleQuotes ${doubleQuotes.toUpperCase()}');

// Multi-line String

print('''With

triple simple quotes

I can define

a string

over multiple

lines''');

// Raw String

print(r'Hello \n $singleQuotes ${doubleQuotes.toUpperCase()}');

Page 34: Introduction to dart par Yohan Beschi

Types - Numbers

2013-04-10 Introduction to Dart 34

num integer = 30;

print('integer is num: ${integer is num}'); // true

print('integer is int: ${integer is int}'); // true

print('integer is double: ${integer is double}'); // false

num intgerToo = 3.0;

print('intgerToo is num: ${intgerToo is num}'); // true

print('intgerToo is int: ${intgerToo is int}'); // true

print('intgerToo is double: ${intgerToo is double}'); // false

num doubl = 1.1;

print('doubl is num: ${doubl is num}'); // true

print('doubl is int: ${doubl is int}'); // false

print('doubl is double: ${doubl is double}'); // true

Page 35: Introduction to dart par Yohan Beschi

Types - Numbers

2013-04-10 Introduction to Dart 35

int realInt = 3;

print('realInt is num: ${realInt is num}'); // true

print('realInt is int: ${realInt is int}'); // true

print('realInt is double: ${realInt is double}'); // false

double realDouble = 1.1;

print('realDouble is num: ${realDouble is num}'); // true

print('realDouble is int: ${realDouble is int}'); // false

print('realDouble is double: ${realDouble is double}'); // true

Page 36: Introduction to dart par Yohan Beschi

Types – Lists

2013-04-10 Introduction to Dart 36

List realList = [1, true, 'String', 5.6e5];

// Creating an extendable list

List dynamicList = new List();

//dynamicList[0] = 1; // throws an exception

dynamicList.add(1);

dynamicList.add(true);

dynamicList.add('String');

dynamicList.add(5.6e5);

// Creating an empty fixed size list

// List fixedList = [null, null, null, null];

List fixedList = new List(4);

//fixedList.add(1); // throws an Exception

fixedList[0] = 1;

fixedList[1] = true;

fixedList[2] = 'String';

fixedList[3] = 5.6e5;

Page 37: Introduction to dart par Yohan Beschi

Types – Maps

2013-04-10 Introduction to Dart 37

Map realMap = {'key': 'value', '1': 1};

Map newMap = new Map();

newMap[1] = true;

newMap['1'] = false;

print(newMap);

print('${newMap[1]} ${newMap['1']}');

Map<String, int> genericMap = new Map();

genericMap['one'] = 1;

genericMap[2] = '2'; // Warning, but doesn't really matter

print(genericMap);

Page 38: Introduction to dart par Yohan Beschi

Conditionals – if/else

2013-04-10 Introduction to Dart 38

if (/* condition */) {

//

} else if (/* other condition */) {

//

} else {

//

}

Page 39: Introduction to dart par Yohan Beschi

Conditionals – switch

2013-04-10 Introduction to Dart 39

switch(variable) {

case 1:

case 2:

//

break;

case 8:

case 9:

//

break;

default:

//

break;

}

Page 40: Introduction to dart par Yohan Beschi

Loops – For in

2013-04-10 Introduction to Dart 40

void main() {

List<int> list = [1, 2, 3, 4, 5, 6, 7, 8, 9];

int sum = 0;

for (int element in list) {

sum += element;

}

assert(sum == 45);

}

Page 41: Introduction to dart par Yohan Beschi

Functions – Top-level functions

2013-04-10 Introduction to Dart 41

void main() {

int squaredNum = square(2);

assert(squaredNum == 4);

}

int square(int i) {

return i * i;

}

Page 42: Introduction to dart par Yohan Beschi

Functions – First-class functions

2013-04-10 Introduction to Dart 42

void main() {

List operation1 = ['+', '5', '2'];

num result1 = compute(operation1);

assert(result1 == 7);

}

num compute(List operation) {

Function operator = findOperator(operation[0]);

double left = double.parse(operation[1]);

double right = double.parse(operation[2]);

return operator(left, right);

}

Page 43: Introduction to dart par Yohan Beschi

Functions – typedef

2013-04-10 Introduction to Dart 43

void main() {

List operation1 = ['+', '5', '2'];

int result1 = computeList(operation1);

assert(result1 == 7);

}

typedef int Operator(num left, num right);

int computeList(List operation) {

Operator operator = findOperator(operation[0]);

int left = int.parse(operation[1]);

int right = int.parse(operation[2]);

return operator(left, right);

}

Page 44: Introduction to dart par Yohan Beschi

Classes abstraites

2013-04-10 Introduction to Dart 44

abstract class Validatable {

}

Page 45: Introduction to dart par Yohan Beschi

Classes abstraites

2013-04-10 Introduction to Dart 45

abstract class Validatable {

List<Object> valuesToValidate();

}

Page 46: Introduction to dart par Yohan Beschi

Classes abstraites

2013-04-10 Introduction to Dart 46

abstract class Validator<T extends Validatable> {

}

Page 47: Introduction to dart par Yohan Beschi

Classes abstraites

2013-04-10 Introduction to Dart 47

abstract class Validator<T extends Validatable> {

bool validate(T object) {

}

}

Page 48: Introduction to dart par Yohan Beschi

Classes abstraites

2013-04-10 Introduction to Dart 48

abstract class Validator<T extends Validatable> {

bool validate(T object) {

for (Object obj in object.valuesToValidate()) {

}

}

}

Page 49: Introduction to dart par Yohan Beschi

Classes abstraites

2013-04-10 Introduction to Dart 49

abstract class Validator<T extends Validatable> {

bool validate(T object) {

for (Object obj in object.valuesToValidate()) {

if (StringUtils.isEmpty(obj.toString())) {

}

}

}

}

Page 50: Introduction to dart par Yohan Beschi

Classes abstraites

2013-04-10 Introduction to Dart 50

abstract class Validator<T extends Validatable> {

bool validate(T object) {

for (Object obj in object.valuesToValidate()) {

if (StringUtils.isEmpty(obj.toString())) {

return false;

}

}

return true;

}

}

Page 51: Introduction to dart par Yohan Beschi

Classes concrètes

2013-04-10 Introduction to Dart 51

class User {

}

Page 52: Introduction to dart par Yohan Beschi

Classes concrètes

2013-04-10 Introduction to Dart 52

class User implements Validatable {

}

Page 53: Introduction to dart par Yohan Beschi

Classes concrètes

2013-04-10 Introduction to Dart 53

class User implements Validatable {

String username;

String password;

}

Page 54: Introduction to dart par Yohan Beschi

Classes concrètes

2013-04-10 Introduction to Dart 54

class User implements Validatable {

String username;

String password;

User(this.username, this.password);

}

Page 55: Introduction to dart par Yohan Beschi

Classes concrètes

2013-04-10 Introduction to Dart 55

class User implements Validatable {

String username;

String password;

User(this.username, this.password);

List<Object> valuesToValidate() {

return [username, password];

}

}

Page 56: Introduction to dart par Yohan Beschi

But there is more…

2013-04-10 Introduction to Dart 56

⦿Mixins

⦿Optionally typed

⦿Top level functions

⦿Mono process

Page 57: Introduction to dart par Yohan Beschi

Dart Reference API

⦿Core

⦿HTML

⦿Async

⦿ IO

⦿Crypto

⦿ JSON

⦿Mirrors

⦿UTF

⦿TU et Mocks

⦿Math

⦿ Logging

⦿URI

⦿ I18N

⦿etc.

2013-04-10 Introduction to Dart 57

Page 58: Introduction to dart par Yohan Beschi

Isolates

2013-04-10 Introduction to Dart 58

Page 59: Introduction to dart par Yohan Beschi

Uses

⦿Single-page Web Apps

⦿Client and server side applications

⦿HTML Games

2013-04-10 Introduction to Dart 59

Page 60: Introduction to dart par Yohan Beschi

Roadmap

2013-04-10 Introduction to Dart 60

Today : M3 ?? : M4 Summer 2013 : V1 !

Page 61: Introduction to dart par Yohan Beschi

UIs

2013-04-10 Introduction to Dart 61

Page 62: Introduction to dart par Yohan Beschi

Objectives <ul>

<li>School 1</li>

<li>School 2

<ul>

<li>Grade 2.1</li>

<li>Grade 2.2

<ul>

<li>Person 2.2.1</li>

<li>Person 2.2.2</li>

</ul>

</li>

</ul>

</li>

<li>School 3

<ul>

<li>Grade 3.1</li>

</ul>

</li>

<li>School 4</li>

</ul>

2013-04-10 Introduction to Dart 62

Page 63: Introduction to dart par Yohan Beschi

Classes

2013-04-10 Introduction to Dart 63

class School {

String schoolName;

List<Grade> grades;

School(this.schoolName,

[this.grades]);

}

class Grade {

String schoolGrade;

List<Student> students;

Grade(this.schoolGrade,

[this.students]);

}

class Student {

String firstname;

String lastname;

Student(this.firstname,

this.lastname);

}

Page 64: Introduction to dart par Yohan Beschi

The Old-Fashioned Way

2013-04-10 Introduction to Dart 64

Page 65: Introduction to dart par Yohan Beschi

The Old-Fashioned Way

2013-04-10 Introduction to Dart 65

void main() {

String tree = '<ul>';

for (School school in schools) {

tree += '<li>${school.schoolName}';

// Grades

tree += '</li>';

}

tree += '</ul>';

query('body').insertAdjacentHtml('afterBegin', tree);

}

Page 66: Introduction to dart par Yohan Beschi

The Old-Fashioned Way

2013-04-10 Introduction to Dart 66

var grades = school.grades;

if (grades != null) {

tree += '<ul>';

for (Grade grade in grades) {

tree += '<li>${grade.schoolGrade}';

// Students

tree += '</li>';

}

tree += '</ul>';

}

Page 67: Introduction to dart par Yohan Beschi

The Old-Fashioned Way

2013-04-10 Introduction to Dart 67

var students = grade.students;

if (students != null) {

tree += '<ul>';

for (Student student in students) {

tree +=

'<li>${student.firstname} ${student.lastname}</li>';

}

tree += '</ul>';

}

Page 68: Introduction to dart par Yohan Beschi

The Old-Fashioned Way

2013-04-10 Introduction to Dart 68

void main() {

String tree = '<ul>';

for (School school in schools) {

tree += '<li>${school.schoolName}';

var grades = school.grades;

if (grades != null) {

tree += '<ul>';

for (Grade grade in grades) {

tree += '<li>${grade.schoolGrade}';

var students = grade.students;

if (students != null) {

tree += '<ul>';

for (Student student in students) {

tree += '<li>${student.firstname}

${student.lastname}</li>';

}

tree += '</ul>';

}

tree += '</li>';

}

tree += '</ul>';

}

tree += '</li>';

}

tree += '</ul>';

query('body')

.insertAdjacentHtml('afterBegin', tree);

}

Page 69: Introduction to dart par Yohan Beschi

Introducing reusable components

2013-04-10 Introduction to Dart 69

Page 70: Introduction to dart par Yohan Beschi

Is there a pattern here ?

2013-04-10 Introduction to Dart 70

var grades = school.grades;

if (grades != null) {

tree += '<ul>';

for (Grade grade in grades) {

tree += '<li>${grade.schoolGrade}';

// Students

tree += '</li>';

}

tree += '</ul>';

}

Page 71: Introduction to dart par Yohan Beschi

Is there a pattern here ?

2013-04-10 Introduction to Dart 71

var grades = school.grades;

if (grades != null) {

tree += '<ul>';

tree += '</ul>';

}

Page 72: Introduction to dart par Yohan Beschi

Is there a pattern here ?

2013-04-10 Introduction to Dart 72

var grades = school.grades;

if (grades != null) {

tree += '<ul>';

tree += '</ul>';

}

for (Grade grade in grades) {

tree += '<li>${grade.schoolGrade}';

tree += '</li>';

}

Page 73: Introduction to dart par Yohan Beschi

Is there a pattern here ?

2013-04-10 Introduction to Dart 73

var grades = school.grades;

if (grades != null) {

tree += '<ul>';

tree += '</ul>';

}

for (Grade grade in grades) {

tree += '<li>${grade.schoolGrade}';

tree += '</li>';

}

// Do the same with children

Page 74: Introduction to dart par Yohan Beschi

Recursive Pattern

2013-04-10 Introduction to Dart 74

String doSomething(/* parameters */) {

String tree = '';

var grades = school.grades;

if (grades != null) {

tree += '<ul>';

for (Grade grade in grades) {

tree += '<li>${grade.schoolGrade}';

tree += doSomething(/* parameters */);

tree += '</li>';

}

tree += '</ul>';

}

return tree;

}

Page 75: Introduction to dart par Yohan Beschi

Side note – Functions & sugar syntax

2013-04-10 Introduction to Dart 75

int length(String s) {

return s.length;

}

Page 76: Introduction to dart par Yohan Beschi

Side note – Functions & sugar syntax

2013-04-10 Introduction to Dart 76

int length(String s) {

return s.length;

}

int length(String s)

=> s.length;

Page 77: Introduction to dart par Yohan Beschi

Easy use of reusable components

2013-04-10 Introduction to Dart 77

void main() {

final Tree tree = new Tree(...);

}

Page 78: Introduction to dart par Yohan Beschi

Easy use of reusable components

2013-04-10 Introduction to Dart 78

void main() {

final Tree tree = new Tree(...);

tree.setData(schools);

tree.addTo('body', 'afterBegin');

}

Page 79: Introduction to dart par Yohan Beschi

Easy use of reusable components

2013-04-10 Introduction to Dart 79

void main() {

final Tree tree = new Tree(

[new TreeConfig((School s) => s.schoolName,

(School s) => s.grades),

new TreeConfig((Grade g) => g.schoolGrade,

(Grade g) => g.students),

new TreeConfig((Student s) =>

'${s.firstname} ${s.lastname}')]

);

tree.setData(schools);

tree.addTo('body', 'afterBegin');

}

Page 80: Introduction to dart par Yohan Beschi

Easy use of reusable components

2013-04-10 Introduction to Dart 80

class School {

String schoolName;

List<Grade> grades;

School(this.schoolName,

[this.grades]);

}

class Grade {

String schoolGrade;

List<Student> students;

Grade(this.schoolGrade,

[this.students]);

}

class Student{

String firstname;

String lastname;

Student(this.firstname,

this.lastname);

}

Page 81: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 81

typedef dynamic Accessor(dynamic data);

class TreeConfig {

Accessor _value;

Accessor _children;

TreeConfig(Accessor this._value,

[Accessor this._children]);

Accessor get value => _value;

Accessor get children => _children;

}

Page 82: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 82

typedef dynamic Accessor(dynamic data);

class TreeConfig {

Accessor _value;

Accessor _children;

TreeConfig(Accessor this._value,

[Accessor this._children]);

Accessor get value => _value;

Accessor get children => _children;

}

Page 83: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 83

typedef dynamic Accessor(dynamic data);

class TreeConfig {

Accessor _value;

Accessor _children;

TreeConfig(Accessor this._value,

[Accessor this._children]);

Accessor get value => _value;

Accessor get children => _children;

}

Page 84: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 84

typedef dynamic Accessor(dynamic data);

class TreeConfig {

Accessor _value;

Accessor _children;

TreeConfig(Accessor this._value,

[Accessor this._children]);

Accessor get value => _value;

Accessor get children => _children;

}

Page 85: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 85

class Tree {

List<TreeConfig> treeConfigs;

String tree;

Tree(this.treeConfigs);

String setData(final List data) {

// Build tree

}

void addTo(String selector,

[String where = 'afterEnd']) {

query(selector).insertAdjacentHtml(where, this.tree);

}

}

Page 86: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 86

class Tree {

List<TreeConfig> treeConfigs;

String tree;

Tree(this.treeConfigs);

String setData(final List data) {

// Build tree

}

void addTo(String selector,

[String where = 'afterEnd']) {

query(selector).insertAdjacentHtml(where, this.tree);

}

}

Page 87: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 87

class Tree {

List<TreeConfig> treeConfigs;

String tree;

Tree(this.treeConfigs);

String setData(final List data) {

// Build tree

}

void addTo(String selector,

[String where = 'afterEnd']) {

query(selector).insertAdjacentHtml(where, this.tree);

}

}

Page 88: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 88

class Tree {

List<TreeConfig> treeConfigs;

String tree;

Tree(this.treeConfigs);

String setData(final List data) {

// Build tree

}

void addTo(String selector,

[String where = 'afterEnd']) {

query(selector).insertAdjacentHtml(where, this.tree);

}

}

Page 89: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 89

class Tree {

List<TreeConfig> treeConfigs;

String tree;

Tree(this.treeConfigs);

String setData(final List data) {

// Build tree

}

void addTo(String selector,

[String where = 'afterEnd']) {

query(selector).insertAdjacentHtml(where, this.tree);

}

}

Page 90: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 90

String buildOneLevelTree(final List data,

final List<TreeConfig> treeNodes,

[final int depth = 0]) {

String tree = '';

if (data != null && !data.isEmpty) {

}

return tree;

}

Page 91: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 91

String buildOneLevelTree(final List data,

final List<TreeConfig> treeNodes,

[final int depth = 0]) {

String tree = '';

if (data != null && !data.isEmpty) {

}

return tree;

}

final TreeConfig treeNode = treeNodes[depth];

tree += '<ul>';

for (dynamic element in data) {

}

tree += '</ul>';

Page 92: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 92

String buildOneLevelTree(final List data,

final List<TreeConfig> treeNodes,

[final int depth = 0]) {

String tree = '';

if (data != null && !data.isEmpty) {

final TreeConfig treeNode = treeNodes[depth];

tree += '<ul>';

for (dynamic element in data) {

}

tree += '</ul>';

}

return tree;

}

tree += '<li>${treeNode.value(element)}';

tree += '</li>';

Page 93: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 93

String buildOneLevelTree(final List data, final List<TreeConfig> treeNodes,

[final int depth = 0]) {

String tree = '';

if (data != null && !data.isEmpty) {

final TreeConfig treeNode = treeNodes[depth];

tree += '<ul>';

for (dynamic element in data) {

tree += '<li>${treeNode.value(element)}';

tree += '</li>';

}

tree += '</ul>';

}

return tree;

}

if (treeNode.children != null) {

tree += buildOneLevelTree(treeNode.children(element),

treeNodes, depth + 1);

}

Page 94: Introduction to dart par Yohan Beschi

Implementing a reusable components

2013-04-10 Introduction to Dart 94

class Tree {

List<TreeConfig> treeConfigs;

String tree;

Tree(this.treeConfigs);

String setData(final List data) {

}

void addTo(String selector,

[String where = 'afterEnd']) {

query(selector).insertAdjacentHtml(where, this.tree);

}

}

this.tree = buildOneLevelTree(data, this.treeConfigs);

return this.tree;

String buildOneLevelTree(final List data,

final List<TreeConfig> treeNodes,

[final int depth = 0]) {

// Implementation

}

Page 95: Introduction to dart par Yohan Beschi

Getting ride of Strings

2013-04-10 Introduction to Dart 95

Element buildOneLevelTree(final List data, final List<TreeConfig> treeNodes,

[final int depth = 0]) {

Element tree; // String tree = '';

if (data != null && !data.isEmpty) {

final TreeConfig treeNode = treeNodes[depth];

tree = new UListElement(); // tree += '<ul>';

for (dynamic element in data) {

final LIElement li = new LIElement(); // <li>;

li.text = treeNode.value(element); // ${treeNode.value(element)}

if (treeNode.children != null) {

final UListElement ulChild = //

buildOneLevelTree(treeNode.children(element), treeNodes, depth + 1);

if (ulChild != null) { //

li.append(ulChild); // tree += buildOneLevelTree(...)

} //

}

tree.append(li); // tree += '<li>${treeNode.value(element)}';

}

}

return tree;

}

Page 96: Introduction to dart par Yohan Beschi

Getting ride of Strings

2013-04-10 Introduction to Dart 96

class Tree {

List<TreeConfig> treeConfigs;

Element tree; // String tree;

Tree(this.treeConfigs);

Element setData(final List data) {

this.tree = buildOneLevelTree(data, this.treeConfigs);

return this.tree;

}

Element buildOneLevelTree(final List data,

final List<TreeConfig> treeNodes,

[final int depth = 0]) {

// Implementation

}

void addTo(String selector,

[String where = 'afterEnd']) {

query(selector).insertAdjacentElement(where, this.tree);

}

}

Page 97: Introduction to dart par Yohan Beschi

web_ui

2013-04-10 Introduction to Dart 97

Page 98: Introduction to dart par Yohan Beschi

web_ui

⦿Based on HTML5 Web Components Spec

⦿Syntax and uses similar to JSP tags

⦿Template Engine – Compilation needed

⦿Reusable components

⦿CSS encapsulation

⦿Data-binding

⦿Complex for real life use-cases

⦿Doesn’t solve layouting problems

2013-04-10 Introduction to Dart 98

Page 99: Introduction to dart par Yohan Beschi

web_ui and Single-Page Webapps

2013-04-10 Introduction to Dart 99

<!DOCTYPE html>

<html>

<head>

<title>01_web_ui</title>

</head>

<body>

<script type="application/dart" src="01_web_ui.dart">

</script>

<script src="packages/browser/dart.js"></script>

</body>

</html>

Page 100: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 100

<!DOCTYPE html>

<html>

<body>

</body>

</html>

Page 101: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 101

<!DOCTYPE html>

<html>

<body>

<element>

</element>

</body>

</html>

Page 102: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 102

<!DOCTYPE html>

<html>

<body>

<element name="x-click-counter">

</element>

</body>

</html>

Page 103: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 103

<!DOCTYPE html>

<html>

<body>

<element name="x-click-counter" constructor="CounterComponent">

</element>

</body>

</html>

Page 104: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 104

<!DOCTYPE html>

<html>

<body>

<element name="x-click-counter" constructor="CounterComponent" extends="div">

</element>

</body>

</html>

Page 105: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 105

<!DOCTYPE html>

<html>

<body>

<element name="x-click-counter" constructor="CounterComponent" extends="div">

<template>

</template>

</element>

</body>

</html>

Page 106: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 106

<!DOCTYPE html>

<html>

<body>

<element name="x-click-counter" constructor="CounterComponent" extends="div">

<template>

<div>

<button>Click me</button><br />

<span>(click count: {{count}})</span>

</div>

</template>

</element>

</body>

</html>

Page 107: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 107

<!DOCTYPE html>

<html>

<body>

<element name="x-click-counter" constructor="CounterComponent" extends="div">

<template>

<div>

<button>Click me</button><br />

<span>(click count: {{count}})</span>

</div>

</template>

</element>

</body>

</html>

Page 108: Introduction to dart par Yohan Beschi

web_ui - The template

2013-04-10 Introduction to Dart 108

<!DOCTYPE html>

<html>

<body>

<element name="x-click-counter" constructor="CounterComponent" extends="div">

<template>

<div>

<button>Click me</button><br />

<span>(click count: {{count}})</span>

</div>

</template>

<script type="application/dart" src="xclickcounter.dart"></script>

</element>

</body>

</html>

Page 109: Introduction to dart par Yohan Beschi

web_ui – Extending WebComponent

2013-04-10 Introduction to Dart 109

class CounterComponent {

}

Page 110: Introduction to dart par Yohan Beschi

web_ui – Extending WebComponent

2013-04-10 Introduction to Dart 110

class CounterComponent extends WebComponent {

}

Page 111: Introduction to dart par Yohan Beschi

web_ui – Extending WebComponent

2013-04-10 Introduction to Dart 111

class CounterComponent extends WebComponent {

@observable

int count = 0;

}

Page 112: Introduction to dart par Yohan Beschi

web_ui – Extending WebComponent

2013-04-10 Introduction to Dart 112

class CounterComponent extends WebComponent {

@observable

int count = 0;

void increment(Event event) {

count++;

}

}

Page 113: Introduction to dart par Yohan Beschi

web_ui – Extending WebComponent

2013-04-10 Introduction to Dart 113

class CounterComponent extends WebComponent {

@observable

int count = 0;

void increment(Event event) {

count++;

}

void inserted() {

this.query('button').onClick.listen(increment);

}

}

Page 114: Introduction to dart par Yohan Beschi

web_ui and Single-Page Webapps

2013-04-10 Introduction to Dart 114

<!DOCTYPE html>

<html>

<head>

<title>01_web_ui</title>

<link rel="components" href="xclickcounter.html">

</head>

<body>

<script type="application/dart" src="01_web_ui.dart">

</script>

<script src="packages/browser/dart.js"></script>

</body>

</html>

Page 115: Introduction to dart par Yohan Beschi

web_ui – The application

2013-04-10 Introduction to Dart 115

void main() {

}

Page 116: Introduction to dart par Yohan Beschi

web_ui – The application

2013-04-10 Introduction to Dart 116

void main() {

var element = new Element.html(

'<x-click-counter id="click_counter"></x-click-counter>'

);

}

Page 117: Introduction to dart par Yohan Beschi

web_ui – The application

2013-04-10 Introduction to Dart 117

void main() {

var element = new Element.html(

'<x-click-counter id="click_counter"></x-click-counter>'

);

var counter = new CounterComponent()

..host = element

..count = 25;

}

Page 118: Introduction to dart par Yohan Beschi

web_ui – The application

2013-04-10 Introduction to Dart 118

void main() {

var element = new Element.html(

'<x-click-counter id="click_counter"></x-click-counter>'

);

var counter = new CounterComponent()

..host = element

..count = 25;

var lifecycleCaller = new ComponentItem(counter)

..create();

query('body').append(counter.host);

lifecycleCaller.insert();

}

Page 119: Introduction to dart par Yohan Beschi

web_ui – The application

2013-04-10 Introduction to Dart 119

void main() {

var element = new Element.html(

'<x-click-counter id="click_counter"></x-click-counter>'

);

var counter = new CounterComponent()

..host = element

..count = 25;

var lifecycleCaller = new ComponentItem(counter)..create();

query('body').append(counter.host);

lifecycleCaller.insert();

var button = new ButtonElement()

..text = 'Update'

..onClick.listen((e) {

counter.count = 100;

watchers.dispatch();

});

query('body').append(button);

}

Page 120: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 120

Page 121: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 121

Page 122: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 122

Page 123: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 123

Page 124: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 124

Page 125: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 125

builder()

..div({'id' : 'banner'})

..div({'id' : 'head'}, 'Dart Playground')

..div({'id' : 'controls'})

..span(null, 'Environment: ')

..addElement(listboxEnv)

..addElement(runButton)

..end() // controls

..end() // banner

..div({'id':'wrap'})

..addElement(e(linedTextarea.element))

..end() // wraps

..addElement(output);

Page 126: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 126

builder()

..div({'id' : 'banner'})

..div({'id' : 'head'}, 'Dart Playground')

..div({'id' : 'controls'})

..span(null, 'Environment: ')

..addElement(listboxEnv)

..addElement(runButton)

..end() // controls

..end() // banner

..div({'id':'wrap'})

..addElement(e(linedTextarea.element))

..end() // wraps

..addElement(output);

Page 127: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 127

builder()

..div({'id' : 'banner'})

..div({'id' : 'head'}, 'Dart Playground')

..div({'id' : 'controls'})

..span(null, 'Environment: ')

..addElement(listboxEnv)

..addElement(runButton)

..end() // controls

..end() // banner

..div({'id':'wrap'})

..addElement(e(linedTextarea.element))

..end() // wraps

..addElement(output);

Page 128: Introduction to dart par Yohan Beschi

A word about Layouts

2013-04-10 Introduction to Dart 128

builder()

..div({'id' : 'banner'})

..div({'id' : 'head'}, 'Dart Playground')

..div({'id' : 'controls'})

..span(null, 'Environment: ')

..addElement(listboxEnv)

..addElement(runButton)

..end() // controls

..end() // banner

..div({'id':'wrap'})

..addElement(e(linedTextarea.element))

..end() // wraps

..addElement(output);

Page 129: Introduction to dart par Yohan Beschi

The Future of Dart ?

2013-04-10 Introduction to Dart 129

Page 130: Introduction to dart par Yohan Beschi

Want to know more ?

DartLangFR ⦿ Mailing-list : dartlangfr (https://groups.google.com/forum/?fromgroups=&hl=en#!forum/dartlangfr)

⦿ Google+ : DartlangFR (https://plus.google.com/u/0/communities/104813951711720144450)

⦿ Twitter : @dartlang_fr ⦿ Blog : dartlangfr.net

DartLang

⦿ Official website: www.dartlang.org ⦿ Mailing-list : dartlang

(https://groups.google.com/a/dartlang.org/forum/?fromgroups&hl=en#!forum/misc)

⦿ Google+ : Dart (https://plus.google.com/+dartlang)

⦿ Google+ : Dartisans (https://plus.google.com/communities/114566943291919232850)

⦿ Twitter : @dart_lang ⦿ Blog : blog.dartwatch.com ⦿ Newsletter : Dart weekly

2013-04-10 Introduction to Dart 130

Page 131: Introduction to dart par Yohan Beschi

Github

⦿Paris JUG ⦿ https://github.com/yohanbeschi/parisjug_20130409.dart

⦿DevoxxFR 2013 ⦿ https://github.com/yohanbeschi/devoxxfr_20130327.dart

⦿Widgets ⦿ https://github.com/yohanbeschi/pwt_proto.dart

⦿Web Editor for Dart ⦿ https://github.com/yohanbeschi/web_editor.dart

2013-04-10 Introduction to Dart 131

Page 132: Introduction to dart par Yohan Beschi

What’s next ?

2013-04-10 Introduction to Dart 132

https://github.com/yohanbeschi/soat_20130410.dart

Page 133: Introduction to dart par Yohan Beschi

Questions ?

2013-04-10 Introduction to Dart 133