Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: samples/third_party/todomvc/web/elements/td_model.dart

Issue 60353013: Update TodoMVC based on https://github.com/polymer/todomvc (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix pubspecs Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 library todomvc.web.elements.td_model;
2
3 import 'dart:html';
4 import 'package:polymer/polymer.dart';
5 import '../lib-elements/polymer_localstorage.dart';
6
7 class Todo extends Observable {
8 @observable String title;
9 @observable bool completed = false;
10
11 Todo(this.title);
12
13 Todo.fromJson(Map json)
14 : title = json['title'], completed = json['completed'];
15
16 Map toJson() => { 'title': title, 'completed': completed };
17
18 String toString() => "$title (${completed ? '' : 'not '}done)";
19 }
20
21 @CustomTag('td-model')
22 class TodoModel extends PolymerElement {
23 @observable ObservableList<Todo> items;
Siggi Cherem (dart-lang) 2013/11/07 02:12:47 should we use @published for the few attributes th
Jennifer Messerly 2013/11/07 04:27:36 yeah, I wondered that too. Polymer-js version isn'
24 @observable Iterable<Todo> filtered;
25 @observable int completedCount = 0;
26 @observable int activeCount = 0;
27 @observable bool allCompleted = false;
28 @observable String storageId;
29 @observable PolymerLocalStorage storage;
30 @observable String filter;
31 @observable String activeItemWord;
32
33 final filters = {
34 'active': (item) => !item.completed,
35 'completed': (item) => item.completed
36 };
37
38 factory TodoModel() => new Element.tag('td-model');
39 TodoModel.created() : super.created();
40
41 void ready() {
42 async((_) {
43 if (items == null) items = new ObservableList<Todo>();
44 });
45 }
46
47 void filterChanged() {
48 filterItems();
49 }
50
51 void itemsChanged() {
52 completedCount = items.where(filters['completed']).length;
53 activeCount = items.length - completedCount;
54 allCompleted = completedCount > 0 && activeCount == 0;
55
56 filterItems();
57 if (storage != null) {
58 storage.value = items;
59 storage.save();
60 }
61
62 // TODO(jmesserly): polymer_expressions lacks ternary operator.
63 activeItemWord = activeCount == 1 ? 'item' : 'items';
64 }
65
66 void storageIdChanged() {
67 storage = document.querySelector('#$storageId');
68 if (storage != null && storage.value != null) {
69 items = toObservable(storage.value.map((i) => new Todo.fromJson(i)));
70 }
71 }
72
73 void filterItems() {
74 var fn = filters[filter];
75 filtered = fn != null ? items.where(fn) : items;
76 }
77
78 void newItem(String title) {
79 title = title.trim();
80 if (title != '') {
81 items.add(new Todo(title));
82 itemsChanged();
83 }
84 }
85
86 void destroyItem(Todo item) {
87 if (items.remove(item)) itemsChanged();
88 }
89
90 void clearItems() {
91 items.removeWhere(filters['completed']);
92 }
93
94 void setItemsCompleted(bool completed) {
95 for (var item in items) {
96 item.completed = completed;
97 }
98 itemsChanged();
99 }
100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698