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

Side by Side Diff: pkg/mdv/test/binding_syntax_test.dart

Issue 19689009: Ensure getInstanceModel is only called when a new instance will be created (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase Created 7 years, 5 months 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
« no previous file with comments | « pkg/mdv/lib/src/template_iterator.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library binding_syntax_test; 5 library binding_syntax_test;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 import 'dart:html'; 9 import 'dart:html';
10 import 'package:mdv/mdv.dart' as mdv; 10 import 'package:mdv/mdv.dart' as mdv;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 expect(div.nodes[2].text, 'b'); 90 expect(div.nodes[2].text, 'b');
91 expect(div.nodes[3].text, 'c'); 91 expect(div.nodes[3].text, 'c');
92 92
93 expect(testSyntax.log, [ 93 expect(testSyntax.log, [
94 [template, model[0]], 94 [template, model[0]],
95 [template, model[1]], 95 [template, model[1]],
96 [template, model[2]], 96 [template, model[2]],
97 ]); 97 ]);
98 }); 98 });
99 99
100 observeTest('getInstanceModel - reorder instances', () {
101 var model = toObservable([0, 1, 2]);
102
103 var div = createTestHtml('<template repeat syntax="Test">{{}}</template>');
104 var template = div.firstChild;
105 var delegate = new TestInstanceModelSyntax();
106
107 recursivelySetTemplateModel(div, model, delegate);
108 performMicrotaskCheckpoint();
109 expect(delegate.count, 3);
110
111 // Note: intentionally mutate in place.
112 model.replaceRange(0, model.length, model.reversed.toList());
113 performMicrotaskCheckpoint();
114 expect(delegate.count, 3);
115 });
116
100 observeTest('Basic', () { 117 observeTest('Basic', () {
101 var model = fooModel(2, 4); 118 var model = fooModel(2, 4);
102 var div = createTestHtml( 119 var div = createTestHtml(
103 '<template bind syntax="2x">' 120 '<template bind syntax="2x">'
104 '{{ foo }} + {{ 2x: bar }} + {{ 4x: bar }}</template>'); 121 '{{ foo }} + {{ 2x: bar }} + {{ 4x: bar }}</template>');
105 recursivelySetTemplateModel(div, model, new TimesTwoSyntax()); 122 recursivelySetTemplateModel(div, model, new TimesTwoSyntax());
106 performMicrotaskCheckpoint(); 123 performMicrotaskCheckpoint();
107 expect(div.nodes.length, 2); 124 expect(div.nodes.length, 2);
108 expect(div.nodes.last.text, '2 + 8 + '); 125 expect(div.nodes.last.text, '2 + 8 + ');
109 126
(...skipping 17 matching lines...) Expand all
127 class TestModelSyntax extends BindingDelegate { 144 class TestModelSyntax extends BindingDelegate {
128 var log = []; 145 var log = [];
129 var altModels = new ListQueue(); 146 var altModels = new ListQueue();
130 147
131 getInstanceModel(template, model) { 148 getInstanceModel(template, model) {
132 log.add([template, model]); 149 log.add([template, model]);
133 return altModels.removeFirst(); 150 return altModels.removeFirst();
134 } 151 }
135 } 152 }
136 153
154 class TestInstanceModelSyntax extends BindingDelegate {
155 int count = 0;
156 getInstanceModel(template, model) {
157 count++;
158 return model;
159 }
160 }
161
137 // Note: this isn't a very smart whitespace handler. A smarter one would only 162 // Note: this isn't a very smart whitespace handler. A smarter one would only
138 // trim indentation, not all whitespace. 163 // trim indentation, not all whitespace.
139 // See "trimOrCompact" in the web_ui Pub package. 164 // See "trimOrCompact" in the web_ui Pub package.
140 class WhitespaceRemover extends BindingDelegate { 165 class WhitespaceRemover extends BindingDelegate {
141 int trimmed = 0; 166 int trimmed = 0;
142 int removed = 0; 167 int removed = 0;
143 168
144 DocumentFragment getInstanceFragment(Element template) { 169 DocumentFragment getInstanceFragment(Element template) {
145 var instance = template.createInstance(); 170 var instance = template.createInstance();
146 var walker = new TreeWalker(instance, NodeFilter.SHOW_TEXT); 171 var walker = new TreeWalker(instance, NodeFilter.SHOW_TEXT);
(...skipping 22 matching lines...) Expand all
169 class TimesTwoSyntax extends BindingDelegate { 194 class TimesTwoSyntax extends BindingDelegate {
170 getBinding(model, path, name, node) { 195 getBinding(model, path, name, node) {
171 path = path.trim(); 196 path = path.trim();
172 if (!path.startsWith('2x:')) return null; 197 if (!path.startsWith('2x:')) return null;
173 198
174 path = path.substring(3); 199 path = path.substring(3);
175 return new CompoundBinding((values) => values['value'] * 2) 200 return new CompoundBinding((values) => values['value'] * 2)
176 ..bind('value', model, path); 201 ..bind('value', model, path);
177 } 202 }
178 } 203 }
OLDNEW
« no previous file with comments | « pkg/mdv/lib/src/template_iterator.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698