開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
Head First JavaScript ―頭とからだで覚えるJavaScriptの基本( Michael Morrison (著), 豊福 剛 (翻訳)、オライリージャパン)の10章(カスタムオブジェクトを作成する)、自分で考えてみよう(p.473)をDartで考えてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
自分で考えてみよう(p.473)
コード
sample.dart
import 'dart:html';
void main () {
run.onClick.listen((MouseEvent event) => Blog.showBlog());
clear.onClick.listen((MouseEvent event) => div.innerHtml = '');
}
ButtonElement run = querySelector('#run_dart');
ButtonElement clear = querySelector('#clear');
DivElement div = querySelector('#d0');
List<Blog> blog = [new Blog(new DateTime(2008, 8, 14), '注文していた…'),
new Blog(new DateTime(2008, 8, 19), '新しいキューブは…'),
new Blog(new DateTime(2008, 8, 16), '新しいキューブと…'),
new Blog(new DateTime(2008, 8, 21), 'ネットで…'),
new Blog()];
String shortFormat(DateTime dt){
return '${dt.month}/${dt.day}/${dt.year}';
}
class Blog implements Comparable{
static const SIGNATURE = 'This blog created by Puzzler Ruby';
static void showBlog([int n]) {
var blog_html = '';
if (n == null){
n = blog.length;
}
blog.sort((x, y) => x.compareTo(y));
bool highlight = true;
blog.forEach((Blog entry){
div.append(entry.toHtml(highlight));
highlight = !highlight;
});
}
DateTime date_time;
String body;
Blog([DateTime date_time, String body]){
this.date_time = date_time != null ? date_time : new DateTime.now();
this.body = body != null ? body : 'Nothing going on today';
}
ParagraphElement toHtml(bool highlight){
ParagraphElement p = new ParagraphElement();
if (highlight) {
p.style.backgroundColor = '#EEEEEE';
}
p.innerHtml = '<strong>${shortFormat(date_time)}</strong><br />' +
'$body<br />$SIGNATURE</em>';
return p;
}
int compareTo(Blog other) => other.date_time.compareTo(date_time);
}
0 コメント:
コメントを投稿