refactor(forEach): change to for-of with iterable

rename: foreach -> for
rename: array -> iterable
update: DartParseTreeWriter
update: naive_infinite_scroll
update: todo
fix: tests in foreach_spec

Closes #919
This commit is contained in:
gdi2290 2015-03-13 16:22:01 -07:00 committed by Misko Hevery
parent f1fca5abb6
commit b61b8d60b7
14 changed files with 80 additions and 76 deletions

View File

@ -18,7 +18,7 @@ export * from './src/change_detection/pipes/pipe';
import {ProtoChangeDetector, DynamicProtoChangeDetector, JitProtoChangeDetector} import {ProtoChangeDetector, DynamicProtoChangeDetector, JitProtoChangeDetector}
from './src/change_detection/proto_change_detector'; from './src/change_detection/proto_change_detector';
import {PipeRegistry} from './src/change_detection/pipes/pipe_registry'; import {PipeRegistry} from './src/change_detection/pipes/pipe_registry';
import {ArrayChangesFactory} from './src/change_detection/pipes/array_changes'; import {IterableChangesFactory} from './src/change_detection/pipes/iterable_changes';
import {KeyValueChangesFactory} from './src/change_detection/pipes/keyvalue_changes'; import {KeyValueChangesFactory} from './src/change_detection/pipes/keyvalue_changes';
import {NullPipeFactory} from './src/change_detection/pipes/null_pipe'; import {NullPipeFactory} from './src/change_detection/pipes/null_pipe';
@ -31,7 +31,7 @@ export class ChangeDetection {
export var defaultPipes = { export var defaultPipes = {
"iterableDiff" : [ "iterableDiff" : [
new ArrayChangesFactory(), new IterableChangesFactory(),
new NullPipeFactory() new NullPipeFactory()
], ],
"keyValDiff" : [ "keyValDiff" : [

View File

@ -1,4 +1,4 @@
export * from './src/directives/foreach'; export * from './src/directives/for';
export * from './src/directives/if'; export * from './src/directives/if';
export * from './src/directives/non_bindable'; export * from './src/directives/non_bindable';
export * from './src/directives/switch'; export * from './src/directives/switch';

View File

@ -16,17 +16,17 @@ import {
import {NO_CHANGE, Pipe} from './pipe'; import {NO_CHANGE, Pipe} from './pipe';
export class ArrayChangesFactory { export class IterableChangesFactory {
supports(obj):boolean { supports(obj):boolean {
return ArrayChanges.supportsObj(obj); return IterableChanges.supportsObj(obj);
} }
create():Pipe { create():Pipe {
return new ArrayChanges(); return new IterableChanges();
} }
} }
export class ArrayChanges extends Pipe { export class IterableChanges extends Pipe {
_collection; _collection;
_length:int; _length:int;
_linkedRecords:_DuplicateMap; _linkedRecords:_DuplicateMap;
@ -66,7 +66,7 @@ export class ArrayChanges extends Pipe {
} }
supports(obj):boolean { supports(obj):boolean {
return ArrayChanges.supportsObj(obj); return IterableChanges.supportsObj(obj);
} }
get collection() { get collection() {

View File

@ -5,12 +5,12 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {ListWrapper} from 'angular2/src/facade/collection'; import {ListWrapper} from 'angular2/src/facade/collection';
@Viewport({ @Viewport({
selector: '[foreach][in]', selector: '[for][of]',
bind: { bind: {
'iterableChanges': 'in | iterableDiff' 'iterableChanges': 'of | iterableDiff'
} }
}) })
export class Foreach { export class For {
viewContainer: ViewContainer; viewContainer: ViewContainer;
constructor(viewContainer:ViewContainer) { constructor(viewContainer:ViewContainer) {
super(); super();
@ -34,13 +34,13 @@ export class Foreach {
(movedRecord) => ListWrapper.push(recordViewTuples, new RecordViewTuple(movedRecord, null)) (movedRecord) => ListWrapper.push(recordViewTuples, new RecordViewTuple(movedRecord, null))
); );
var insertTuples = Foreach.bulkRemove(recordViewTuples, this.viewContainer); var insertTuples = For.bulkRemove(recordViewTuples, this.viewContainer);
changes.forEachAddedItem( changes.forEachAddedItem(
(addedRecord) => ListWrapper.push(insertTuples, new RecordViewTuple(addedRecord, null)) (addedRecord) => ListWrapper.push(insertTuples, new RecordViewTuple(addedRecord, null))
); );
Foreach.bulkInsert(insertTuples, this.viewContainer); For.bulkInsert(insertTuples, this.viewContainer);
for (var i = 0; i < insertTuples.length; i++) { for (var i = 0; i < insertTuples.length; i++) {
this.perViewChange(insertTuples[i].view, insertTuples[i].record); this.perViewChange(insertTuples[i].view, insertTuples[i].record);

View File

@ -1,11 +1,11 @@
import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib'; import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
import {ArrayChanges} from 'angular2/src/change_detection/pipes/array_changes'; import {IterableChanges} from 'angular2/src/change_detection/pipes/iterable_changes';
import {NumberWrapper} from 'angular2/src/facade/lang'; import {NumberWrapper} from 'angular2/src/facade/lang';
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {TestIterable} from './iterable'; import {TestIterable} from './iterable';
import {arrayChangesAsString} from './util'; import {iterableChangesAsString} from './util';
// todo(vicb): UnmodifiableListView / frozen object when implemented // todo(vicb): UnmodifiableListView / frozen object when implemented
export function main() { export function main() {
@ -15,7 +15,7 @@ export function main() {
var l; var l;
beforeEach(() => { beforeEach(() => {
changes = new ArrayChanges(); changes = new IterableChanges();
}); });
afterEach(() => { afterEach(() => {
@ -23,30 +23,30 @@ export function main() {
}); });
it('should support list and iterables', () => { it('should support list and iterables', () => {
expect(ArrayChanges.supportsObj([])).toBeTruthy(); expect(IterableChanges.supportsObj([])).toBeTruthy();
expect(ArrayChanges.supportsObj(new TestIterable())).toBeTruthy(); expect(IterableChanges.supportsObj(new TestIterable())).toBeTruthy();
expect(ArrayChanges.supportsObj(MapWrapper.create())).toBeFalsy(); expect(IterableChanges.supportsObj(MapWrapper.create())).toBeFalsy();
expect(ArrayChanges.supportsObj(null)).toBeFalsy(); expect(IterableChanges.supportsObj(null)).toBeFalsy();
}); });
it('should support iterables', () => { it('should support iterables', () => {
l = new TestIterable(); l = new TestIterable();
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: [] collection: []
})); }));
l.list = [1]; l.list = [1];
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['1[null->0]'], collection: ['1[null->0]'],
additions: ['1[null->0]'] additions: ['1[null->0]']
})); }));
l.list = [2, 1]; l.list = [2, 1];
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[null->0]', '1[0->1]'], collection: ['2[null->0]', '1[0->1]'],
previous: ['1[0->1]'], previous: ['1[0->1]'],
additions: ['2[null->0]'], additions: ['2[null->0]'],
@ -57,20 +57,20 @@ export function main() {
it('should detect additions', () => { it('should detect additions', () => {
l = []; l = [];
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: [] collection: []
})); }));
ListWrapper.push(l, 'a'); ListWrapper.push(l, 'a');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a[null->0]'], collection: ['a[null->0]'],
additions: ['a[null->0]'] additions: ['a[null->0]']
})); }));
ListWrapper.push(l, 'b'); ListWrapper.push(l, 'b');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]'], collection: ['a', 'b[null->1]'],
previous: ['a'], previous: ['a'],
additions: ['b[null->1]'] additions: ['b[null->1]']
@ -83,7 +83,7 @@ export function main() {
l = [1, 0]; l = [1, 0];
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['1[null->0]', '0[0->1]'], collection: ['1[null->0]', '0[0->1]'],
previous: ['0[0->1]'], previous: ['0[0->1]'],
additions: ['1[null->0]'], additions: ['1[null->0]'],
@ -92,7 +92,7 @@ export function main() {
l = [2, 1, 0]; l = [2, 1, 0];
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[null->0]', '1[0->1]', '0[1->2]'], collection: ['2[null->0]', '1[0->1]', '0[1->2]'],
previous: ['1[0->1]', '0[1->2]'], previous: ['1[0->1]', '0[1->2]'],
additions: ['2[null->0]'], additions: ['2[null->0]'],
@ -108,7 +108,7 @@ export function main() {
ListWrapper.push(l, 2); ListWrapper.push(l, 2);
ListWrapper.push(l, 1); ListWrapper.push(l, 1);
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[1->0]', '1[0->1]'], collection: ['2[1->0]', '1[0->1]'],
previous: ['1[0->1]', '2[1->0]'], previous: ['1[0->1]', '2[1->0]'],
moves: ['2[1->0]', '1[0->1]'] moves: ['2[1->0]', '1[0->1]']
@ -122,7 +122,7 @@ export function main() {
ListWrapper.removeAt(l, 1); ListWrapper.removeAt(l, 1);
ListWrapper.insert(l, 0, 'b'); ListWrapper.insert(l, 0, 'b');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[1->0]', 'a[0->1]', 'c'], collection: ['b[1->0]', 'a[0->1]', 'c'],
previous: ['a[0->1]', 'b[1->0]', 'c'], previous: ['a[0->1]', 'b[1->0]', 'c'],
moves: ['b[1->0]', 'a[0->1]'] moves: ['b[1->0]', 'a[0->1]']
@ -131,7 +131,7 @@ export function main() {
ListWrapper.removeAt(l, 1); ListWrapper.removeAt(l, 1);
ListWrapper.push(l, 'a'); ListWrapper.push(l, 'a');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b', 'c[2->1]', 'a[1->2]'], collection: ['b', 'c[2->1]', 'a[1->2]'],
previous: ['b', 'a[1->2]', 'c[2->1]'], previous: ['b', 'a[1->2]', 'c[2->1]'],
moves: ['c[2->1]', 'a[1->2]'] moves: ['c[2->1]', 'a[1->2]']
@ -144,14 +144,14 @@ export function main() {
ListWrapper.push(l, 'a'); ListWrapper.push(l, 'a');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a[null->0]'], collection: ['a[null->0]'],
additions: ['a[null->0]'] additions: ['a[null->0]']
})); }));
ListWrapper.push(l, 'b'); ListWrapper.push(l, 'b');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]'], collection: ['a', 'b[null->1]'],
previous: ['a'], previous: ['a'],
additions: ['b[null->1]'] additions: ['b[null->1]']
@ -160,7 +160,7 @@ export function main() {
ListWrapper.push(l, 'c'); ListWrapper.push(l, 'c');
ListWrapper.push(l, 'd'); ListWrapper.push(l, 'd');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b', 'c[null->2]', 'd[null->3]'], collection: ['a', 'b', 'c[null->2]', 'd[null->3]'],
previous: ['a', 'b'], previous: ['a', 'b'],
additions: ['c[null->2]', 'd[null->3]'] additions: ['c[null->2]', 'd[null->3]']
@ -168,7 +168,7 @@ export function main() {
ListWrapper.removeAt(l, 2); ListWrapper.removeAt(l, 2);
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b', 'd[3->2]'], collection: ['a', 'b', 'd[3->2]'],
previous: ['a', 'b', 'c[2->null]', 'd[3->2]'], previous: ['a', 'b', 'c[2->null]', 'd[3->2]'],
moves: ['d[3->2]'], moves: ['d[3->2]'],
@ -181,7 +181,7 @@ export function main() {
ListWrapper.push(l, 'b'); ListWrapper.push(l, 'b');
ListWrapper.push(l, 'a'); ListWrapper.push(l, 'a');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['d[2->0]', 'c[null->1]', 'b[1->2]', 'a[0->3]'], collection: ['d[2->0]', 'c[null->1]', 'b[1->2]', 'a[0->3]'],
previous: ['a[0->3]', 'b[1->2]', 'd[2->0]'], previous: ['a[0->3]', 'b[1->2]', 'd[2->0]'],
additions: ['c[null->1]'], additions: ['c[null->1]'],
@ -197,7 +197,7 @@ export function main() {
var oo = 'oo'; var oo = 'oo';
ListWrapper.set(l, 1, b + oo); ListWrapper.set(l, 1, b + oo);
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'boo'], collection: ['a', 'boo'],
previous: ['a', 'boo'] previous: ['a', 'boo']
})); }));
@ -207,7 +207,7 @@ export function main() {
l = [NumberWrapper.NaN]; l = [NumberWrapper.NaN];
changes.check(l); changes.check(l);
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: [NumberWrapper.NaN], collection: [NumberWrapper.NaN],
previous: [NumberWrapper.NaN] previous: [NumberWrapper.NaN]
})); }));
@ -219,7 +219,7 @@ export function main() {
ListWrapper.insert(l, 0, 'foo'); ListWrapper.insert(l, 0, 'foo');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['foo[null->0]', 'NaN[0->1]', 'NaN[1->2]'], collection: ['foo[null->0]', 'NaN[0->1]', 'NaN[1->2]'],
previous: ['NaN[0->1]', 'NaN[1->2]'], previous: ['NaN[0->1]', 'NaN[1->2]'],
additions: ['foo[null->0]'], additions: ['foo[null->0]'],
@ -233,7 +233,7 @@ export function main() {
ListWrapper.removeAt(l, 1); ListWrapper.removeAt(l, 1);
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'c[2->1]'], collection: ['a', 'c[2->1]'],
previous: ['a', 'b[1->null]', 'c[2->1]'], previous: ['a', 'b[1->null]', 'c[2->1]'],
moves: ['c[2->1]'], moves: ['c[2->1]'],
@ -242,7 +242,7 @@ export function main() {
ListWrapper.insert(l, 1, 'b'); ListWrapper.insert(l, 1, 'b');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]', 'c[1->2]'], collection: ['a', 'b[null->1]', 'c[1->2]'],
previous: ['a', 'c[1->2]'], previous: ['a', 'c[1->2]'],
additions: ['b[null->1]'], additions: ['b[null->1]'],
@ -256,7 +256,7 @@ export function main() {
ListWrapper.removeAt(l, 0); ListWrapper.removeAt(l, 0);
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'a', 'b[3->2]', 'b[4->3]'], collection: ['a', 'a', 'b[3->2]', 'b[4->3]'],
previous: ['a', 'a', 'a[2->null]', 'b[3->2]', 'b[4->3]'], previous: ['a', 'a', 'a[2->null]', 'b[3->2]', 'b[4->3]'],
moves: ['b[3->2]', 'b[4->3]'], moves: ['b[3->2]', 'b[4->3]'],
@ -270,7 +270,7 @@ export function main() {
ListWrapper.insert(l, 0, 'b'); ListWrapper.insert(l, 0, 'b');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[2->0]', 'a[0->1]', 'a[1->2]', 'b', 'b[null->4]'], collection: ['b[2->0]', 'a[0->1]', 'a[1->2]', 'b', 'b[null->4]'],
previous: ['a[0->1]', 'a[1->2]', 'b[2->0]', 'b'], previous: ['a[0->1]', 'a[1->2]', 'b[2->0]', 'b'],
additions: ['b[null->4]'], additions: ['b[null->4]'],
@ -287,7 +287,7 @@ export function main() {
ListWrapper.push(l, 'a'); ListWrapper.push(l, 'a');
ListWrapper.push(l, 'c'); ListWrapper.push(l, 'c');
changes.check(l); changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({ expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[1->0]', 'a[0->1]', 'c'], collection: ['b[1->0]', 'a[0->1]', 'c'],
previous: ['a[0->1]', 'b[1->0]', 'c'], previous: ['a[0->1]', 'b[1->0]', 'c'],
moves: ['b[1->0]', 'a[0->1]'] moves: ['b[1->0]', 'a[0->1]']

View File

@ -1,6 +1,6 @@
import {isBlank} from 'angular2/src/facade/lang'; import {isBlank} from 'angular2/src/facade/lang';
export function arrayChangesAsString({collection, previous, additions, moves, removals}) { export function iterableChangesAsString({collection, previous, additions, moves, removals}) {
if (isBlank(collection)) collection = []; if (isBlank(collection)) collection = [];
if (isBlank(previous)) previous = []; if (isBlank(previous)) previous = [];
if (isBlank(additions)) additions = []; if (isBlank(additions)) additions = [];

View File

@ -25,12 +25,12 @@ import {Decorator, Component, Viewport} from 'angular2/src/core/annotations/anno
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock'; import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
import {Foreach} from 'angular2/src/directives/foreach'; import {For} from 'angular2/src/directives/for';
import {bind} from 'angular2/di'; import {bind} from 'angular2/di';
export function main() { export function main() {
describe('foreach', () => { describe('for', () => {
var view, cd, compiler, component, tplResolver; var view, cd, compiler, component, tplResolver;
beforeEachBindings(() => [ beforeEachBindings(() => [
@ -52,13 +52,13 @@ export function main() {
function compileWithTemplate(html) { function compileWithTemplate(html) {
var template = new Template({ var template = new Template({
inline: html, inline: html,
directives: [Foreach] directives: [For]
}); });
tplResolver.setTemplate(TestComponent, template); tplResolver.setTemplate(TestComponent, template);
return compiler.compile(TestComponent); return compiler.compile(TestComponent);
} }
var TEMPLATE = '<div><copy-me template="foreach #item in items">{{item.toString()}};</copy-me></div>'; var TEMPLATE = '<div><copy-me template="for #item of items">{{item.toString()}};</copy-me></div>';
it('should reflect initial elements', inject([AsyncTestCompleter], (async) => { it('should reflect initial elements', inject([AsyncTestCompleter], (async) => {
compileWithTemplate(TEMPLATE).then((pv) => { compileWithTemplate(TEMPLATE).then((pv) => {
@ -124,8 +124,8 @@ export function main() {
}); });
})); }));
it('should iterate over an array of objects', () => { it('should iterate over an array of objects', inject([AsyncTestCompleter], (async) => {
compileWithTemplate('<ul><li template="foreach #item in items">{{item["name"]}};</li></ul>').then((pv) => { compileWithTemplate('<ul><li template="for #item of items">{{item["name"]}};</li></ul>').then((pv) => {
createView(pv); createView(pv);
// INIT // INIT
@ -145,11 +145,12 @@ export function main() {
cd.detectChanges(); cd.detectChanges();
expect(DOM.getText(view.nodes[0])).toEqual('shyam;'); expect(DOM.getText(view.nodes[0])).toEqual('shyam;');
async.done();
}); });
}); }));
it('should gracefully handle nulls', inject([AsyncTestCompleter], (async) => { it('should gracefully handle nulls', inject([AsyncTestCompleter], (async) => {
compileWithTemplate('<ul><li template="foreach #item in null">{{item}};</li></ul>').then((pv) => { compileWithTemplate('<ul><li template="for #item of null">{{item}};</li></ul>').then((pv) => {
createView(pv); createView(pv);
cd.detectChanges(); cd.detectChanges();
expect(DOM.getText(view.nodes[0])).toEqual(''); expect(DOM.getText(view.nodes[0])).toEqual('');
@ -199,10 +200,13 @@ export function main() {
it('should repeat over nested arrays', inject([AsyncTestCompleter], (async) => { it('should repeat over nested arrays', inject([AsyncTestCompleter], (async) => {
compileWithTemplate( compileWithTemplate(
'<div><div template="foreach #item in items">' + '<div>'+
'<div template="foreach #subitem in item">' + '<div template="for #item of items">' +
'{{subitem}}-{{item.length}};' + '<div template="for #subitem of item">' +
'</div>|</div></div>' '{{subitem}}-{{item.length}};' +
'</div>|'+
'</div>'+
'</div>'
).then((pv) => { ).then((pv) => {
createView(pv); createView(pv);
component.items = [['a', 'b'], ['c']]; component.items = [['a', 'b'], ['c']];
@ -216,7 +220,7 @@ export function main() {
it('should display indices correctly', inject([AsyncTestCompleter], (async) => { it('should display indices correctly', inject([AsyncTestCompleter], (async) => {
var INDEX_TEMPLATE = var INDEX_TEMPLATE =
'<div><copy-me template="foreach: var item in items; var i=index">{{i.toString()}}</copy-me></div>'; '<div><copy-me template="for: var item of items; var i=index">{{i.toString()}}</copy-me></div>';
compileWithTemplate(INDEX_TEMPLATE).then((pv) => { compileWithTemplate(INDEX_TEMPLATE).then((pv) => {
createView(pv); createView(pv);
component.items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; component.items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

View File

@ -6,7 +6,7 @@ import {bootstrap, Component, Viewport, Template, ViewContainer, Compiler}
import {PromiseWrapper} from 'angular2/src/facade/async'; import {PromiseWrapper} from 'angular2/src/facade/async';
import {ListWrapper} from 'angular2/src/facade/collection'; import {ListWrapper} from 'angular2/src/facade/collection';
import {ScrollAreaComponent} from './scroll_area'; import {ScrollAreaComponent} from './scroll_area';
import {If, Foreach} from 'angular2/directives'; import {If, For} from 'angular2/directives';
import {DOM} from 'angular2/src/dom/dom_adapter'; import {DOM} from 'angular2/src/dom/dom_adapter';
import {document} from 'angular2/src/facade/browser'; import {document} from 'angular2/src/facade/browser';
@ -85,7 +85,7 @@ export function setupReflectorForApp() {
'annotations': [ 'annotations': [
new Component({selector: 'scroll-app'}), new Component({selector: 'scroll-app'}),
new Template({ new Template({
directives: [ScrollAreaComponent, If, Foreach], directives: [ScrollAreaComponent, If, For],
inline: ` inline: `
<div> <div>
<div style="display: flex"> <div style="display: flex">
@ -93,7 +93,7 @@ export function setupReflectorForApp() {
</div> </div>
<div template="if scrollAreas.length > 0"> <div template="if scrollAreas.length > 0">
<p>Following tables are only here to add weight to the UI:</p> <p>Following tables are only here to add weight to the UI:</p>
<scroll-area template="foreach #scrollArea in scrollAreas"></scroll-area> <scroll-area template="for #scrollArea of scrollAreas"></scroll-area>
</div> </div>
</div>` </div>`
})] })]

View File

@ -7,7 +7,7 @@ import {PromiseWrapper} from 'angular2/src/facade/async';
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {Company, Opportunity, Offering, Account, CustomDate, STATUS_LIST} import {Company, Opportunity, Offering, Account, CustomDate, STATUS_LIST}
from './common'; from './common';
import {Foreach} from 'angular2/directives'; import {For} from 'angular2/directives';
export class HasStyle { export class HasStyle {
style:Map; style:Map;
@ -163,10 +163,10 @@ export function setupReflectorForCells() {
} }
}), }),
new Template({ new Template({
directives: [Foreach], directives: [For],
inline: ` inline: `
<div [style]="style"> <div [style]="style">
<button template="foreach #stage in stages" <button template="for #stage of stages"
[disabled]="stage.isDisabled" [disabled]="stage.isDisabled"
[style]="stage.style" [style]="stage.style"
on-click="setStage(stage)"> on-click="setStage(stage)">

View File

@ -25,7 +25,7 @@ import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mappe
import {StyleInliner} from 'angular2/src/core/compiler/style_inliner'; import {StyleInliner} from 'angular2/src/core/compiler/style_inliner';
import {CssProcessor} from 'angular2/src/core/compiler/css_processor'; import {CssProcessor} from 'angular2/src/core/compiler/css_processor';
import {If, Foreach} from 'angular2/directives'; import {If, For} from 'angular2/directives';
import {App, setupReflectorForApp} from './app'; import {App, setupReflectorForApp} from './app';
import {ScrollAreaComponent, setupReflectorForScrollArea} from './scroll_area'; import {ScrollAreaComponent, setupReflectorForScrollArea} from './scroll_area';
import {ScrollItemComponent, setupReflectorForScrollItem} from './scroll_item'; import {ScrollItemComponent, setupReflectorForScrollItem} from './scroll_item';
@ -170,13 +170,13 @@ export function setupReflectorForAngular() {
})] })]
}); });
reflector.registerType(Foreach, { reflector.registerType(For, {
'factory': (vp) => new Foreach(vp), 'factory': (vp) => new For(vp),
'parameters': [[ViewContainer]], 'parameters': [[ViewContainer]],
'annotations' : [new Viewport({ 'annotations' : [new Viewport({
selector: '[foreach]', selector: '[for]',
bind: { bind: {
'iterableChanges': 'in | iterableDiff' 'iterableChanges': 'of | iterableDiff'
} }
})] })]
}); });

View File

@ -12,7 +12,7 @@ import {Offering, ITEMS, ITEM_HEIGHT, VISIBLE_ITEMS, VIEW_PORT_HEIGHT,
ROW_WIDTH, HEIGHT} from './common'; ROW_WIDTH, HEIGHT} from './common';
import {generateOfferings} from './random_data'; import {generateOfferings} from './random_data';
import {ScrollItemComponent} from './scroll_item'; import {ScrollItemComponent} from './scroll_item';
import {Foreach} from 'angular2/directives'; import {For} from 'angular2/directives';
export class ScrollAreaComponent { export class ScrollAreaComponent {
_fullList:List<Offering>; _fullList:List<Offering>;
@ -69,7 +69,7 @@ export function setupReflectorForScrollArea() {
selector: 'scroll-area', selector: 'scroll-area',
}), }),
new Template({ new Template({
directives: [ScrollItemComponent, Foreach], directives: [ScrollItemComponent, For],
inline: ` inline: `
<div> <div>
<div id="scrollDiv" <div id="scrollDiv"
@ -78,7 +78,7 @@ export function setupReflectorForScrollArea() {
<div id="padding"></div> <div id="padding"></div>
<div id="inner"> <div id="inner">
<scroll-item <scroll-item
template="foreach #item in visibleItems" template="for #item of visibleItems"
[offering]="item"> [offering]="item">
</scroll-item> </scroll-item>
</div> </div>

View File

@ -1,4 +1,4 @@
import {bootstrap, Component, Template, Foreach} from 'angular2/angular2'; import {bootstrap, Component, Template, For} from 'angular2/angular2';
import {Store, Todo, TodoFactory} from './services/TodoStore'; import {Store, Todo, TodoFactory} from './services/TodoStore';
@Component({ @Component({
@ -10,7 +10,7 @@ import {Store, Todo, TodoFactory} from './services/TodoStore';
}) })
@Template({ @Template({
url: 'todo.html', url: 'todo.html',
directives: [Foreach] directives: [For]
}) })
class TodoApp { class TodoApp {
todoStore: Store; todoStore: Store;

View File

@ -18,7 +18,7 @@
<ul id="todo-list"> <ul id="todo-list">
<li *foreach="#todo in todoStore.list"> <li *for="#todo of todoStore.list">
<div class="view" <div class="view"
[class.hidden]="todoEdit == todo"> [class.hidden]="todoEdit == todo">

View File

@ -531,4 +531,4 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
} }
// see: https://www.dartlang.org/docs/dart-up-and-running/ch02.html for a full list. // see: https://www.dartlang.org/docs/dart-up-and-running/ch02.html for a full list.
const DART_RESERVED_WORDS = ['if', 'switch']; const DART_RESERVED_WORDS = ['if', 'switch', 'for'];