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}
from './src/change_detection/proto_change_detector';
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 {NullPipeFactory} from './src/change_detection/pipes/null_pipe';
@ -31,7 +31,7 @@ export class ChangeDetection {
export var defaultPipes = {
"iterableDiff" : [
new ArrayChangesFactory(),
new IterableChangesFactory(),
new NullPipeFactory()
],
"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/non_bindable';
export * from './src/directives/switch';

View File

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

View File

@ -5,12 +5,12 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {ListWrapper} from 'angular2/src/facade/collection';
@Viewport({
selector: '[foreach][in]',
selector: '[for][of]',
bind: {
'iterableChanges': 'in | iterableDiff'
'iterableChanges': 'of | iterableDiff'
}
})
export class Foreach {
export class For {
viewContainer: ViewContainer;
constructor(viewContainer:ViewContainer) {
super();
@ -34,13 +34,13 @@ export class Foreach {
(movedRecord) => ListWrapper.push(recordViewTuples, new RecordViewTuple(movedRecord, null))
);
var insertTuples = Foreach.bulkRemove(recordViewTuples, this.viewContainer);
var insertTuples = For.bulkRemove(recordViewTuples, this.viewContainer);
changes.forEachAddedItem(
(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++) {
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 {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 {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {TestIterable} from './iterable';
import {arrayChangesAsString} from './util';
import {iterableChangesAsString} from './util';
// todo(vicb): UnmodifiableListView / frozen object when implemented
export function main() {
@ -15,7 +15,7 @@ export function main() {
var l;
beforeEach(() => {
changes = new ArrayChanges();
changes = new IterableChanges();
});
afterEach(() => {
@ -23,30 +23,30 @@ export function main() {
});
it('should support list and iterables', () => {
expect(ArrayChanges.supportsObj([])).toBeTruthy();
expect(ArrayChanges.supportsObj(new TestIterable())).toBeTruthy();
expect(ArrayChanges.supportsObj(MapWrapper.create())).toBeFalsy();
expect(ArrayChanges.supportsObj(null)).toBeFalsy();
expect(IterableChanges.supportsObj([])).toBeTruthy();
expect(IterableChanges.supportsObj(new TestIterable())).toBeTruthy();
expect(IterableChanges.supportsObj(MapWrapper.create())).toBeFalsy();
expect(IterableChanges.supportsObj(null)).toBeFalsy();
});
it('should support iterables', () => {
l = new TestIterable();
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: []
}));
l.list = [1];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['1[null->0]'],
additions: ['1[null->0]']
}));
l.list = [2, 1];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[null->0]', '1[0->1]'],
previous: ['1[0->1]'],
additions: ['2[null->0]'],
@ -57,20 +57,20 @@ export function main() {
it('should detect additions', () => {
l = [];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: []
}));
ListWrapper.push(l, 'a');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a[null->0]'],
additions: ['a[null->0]']
}));
ListWrapper.push(l, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]'],
previous: ['a'],
additions: ['b[null->1]']
@ -83,7 +83,7 @@ export function main() {
l = [1, 0];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['1[null->0]', '0[0->1]'],
previous: ['0[0->1]'],
additions: ['1[null->0]'],
@ -92,7 +92,7 @@ export function main() {
l = [2, 1, 0];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[null->0]', '1[0->1]', '0[1->2]'],
previous: ['1[0->1]', '0[1->2]'],
additions: ['2[null->0]'],
@ -108,7 +108,7 @@ export function main() {
ListWrapper.push(l, 2);
ListWrapper.push(l, 1);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[1->0]', '1[0->1]'],
previous: ['1[0->1]', '2[1->0]'],
moves: ['2[1->0]', '1[0->1]']
@ -122,7 +122,7 @@ export function main() {
ListWrapper.removeAt(l, 1);
ListWrapper.insert(l, 0, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[1->0]', 'a[0->1]', 'c'],
previous: ['a[0->1]', 'b[1->0]', 'c'],
moves: ['b[1->0]', 'a[0->1]']
@ -131,7 +131,7 @@ export function main() {
ListWrapper.removeAt(l, 1);
ListWrapper.push(l, 'a');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b', 'c[2->1]', 'a[1->2]'],
previous: ['b', 'a[1->2]', 'c[2->1]'],
moves: ['c[2->1]', 'a[1->2]']
@ -144,14 +144,14 @@ export function main() {
ListWrapper.push(l, 'a');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a[null->0]'],
additions: ['a[null->0]']
}));
ListWrapper.push(l, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]'],
previous: ['a'],
additions: ['b[null->1]']
@ -160,7 +160,7 @@ export function main() {
ListWrapper.push(l, 'c');
ListWrapper.push(l, 'd');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b', 'c[null->2]', 'd[null->3]'],
previous: ['a', 'b'],
additions: ['c[null->2]', 'd[null->3]']
@ -168,7 +168,7 @@ export function main() {
ListWrapper.removeAt(l, 2);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b', 'd[3->2]'],
previous: ['a', 'b', 'c[2->null]', 'd[3->2]'],
moves: ['d[3->2]'],
@ -181,7 +181,7 @@ export function main() {
ListWrapper.push(l, 'b');
ListWrapper.push(l, 'a');
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]'],
previous: ['a[0->3]', 'b[1->2]', 'd[2->0]'],
additions: ['c[null->1]'],
@ -197,7 +197,7 @@ export function main() {
var oo = 'oo';
ListWrapper.set(l, 1, b + oo);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'boo'],
previous: ['a', 'boo']
}));
@ -207,7 +207,7 @@ export function main() {
l = [NumberWrapper.NaN];
changes.check(l);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: [NumberWrapper.NaN],
previous: [NumberWrapper.NaN]
}));
@ -219,7 +219,7 @@ export function main() {
ListWrapper.insert(l, 0, 'foo');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['foo[null->0]', 'NaN[0->1]', 'NaN[1->2]'],
previous: ['NaN[0->1]', 'NaN[1->2]'],
additions: ['foo[null->0]'],
@ -233,7 +233,7 @@ export function main() {
ListWrapper.removeAt(l, 1);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'c[2->1]'],
previous: ['a', 'b[1->null]', 'c[2->1]'],
moves: ['c[2->1]'],
@ -242,7 +242,7 @@ export function main() {
ListWrapper.insert(l, 1, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]', 'c[1->2]'],
previous: ['a', 'c[1->2]'],
additions: ['b[null->1]'],
@ -256,7 +256,7 @@ export function main() {
ListWrapper.removeAt(l, 0);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'a', '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]'],
@ -270,7 +270,7 @@ export function main() {
ListWrapper.insert(l, 0, 'b');
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]'],
previous: ['a[0->1]', 'a[1->2]', 'b[2->0]', 'b'],
additions: ['b[null->4]'],
@ -287,7 +287,7 @@ export function main() {
ListWrapper.push(l, 'a');
ListWrapper.push(l, 'c');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[1->0]', 'a[0->1]', 'c'],
previous: ['a[0->1]', 'b[1->0]', 'c'],
moves: ['b[1->0]', 'a[0->1]']

View File

@ -1,6 +1,6 @@
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(previous)) previous = [];
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 {Foreach} from 'angular2/src/directives/foreach';
import {For} from 'angular2/src/directives/for';
import {bind} from 'angular2/di';
export function main() {
describe('foreach', () => {
describe('for', () => {
var view, cd, compiler, component, tplResolver;
beforeEachBindings(() => [
@ -52,13 +52,13 @@ export function main() {
function compileWithTemplate(html) {
var template = new Template({
inline: html,
directives: [Foreach]
directives: [For]
});
tplResolver.setTemplate(TestComponent, template);
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) => {
compileWithTemplate(TEMPLATE).then((pv) => {
@ -124,8 +124,8 @@ export function main() {
});
}));
it('should iterate over an array of objects', () => {
compileWithTemplate('<ul><li template="foreach #item in items">{{item["name"]}};</li></ul>').then((pv) => {
it('should iterate over an array of objects', inject([AsyncTestCompleter], (async) => {
compileWithTemplate('<ul><li template="for #item of items">{{item["name"]}};</li></ul>').then((pv) => {
createView(pv);
// INIT
@ -145,11 +145,12 @@ export function main() {
cd.detectChanges();
expect(DOM.getText(view.nodes[0])).toEqual('shyam;');
async.done();
});
});
}));
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);
cd.detectChanges();
expect(DOM.getText(view.nodes[0])).toEqual('');
@ -199,10 +200,13 @@ export function main() {
it('should repeat over nested arrays', inject([AsyncTestCompleter], (async) => {
compileWithTemplate(
'<div><div template="foreach #item in items">' +
'<div template="foreach #subitem in item">' +
'{{subitem}}-{{item.length}};' +
'</div>|</div></div>'
'<div>'+
'<div template="for #item of items">' +
'<div template="for #subitem of item">' +
'{{subitem}}-{{item.length}};' +
'</div>|'+
'</div>'+
'</div>'
).then((pv) => {
createView(pv);
component.items = [['a', 'b'], ['c']];
@ -216,7 +220,7 @@ export function main() {
it('should display indices correctly', inject([AsyncTestCompleter], (async) => {
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) => {
createView(pv);
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 {ListWrapper} from 'angular2/src/facade/collection';
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 {document} from 'angular2/src/facade/browser';
@ -85,7 +85,7 @@ export function setupReflectorForApp() {
'annotations': [
new Component({selector: 'scroll-app'}),
new Template({
directives: [ScrollAreaComponent, If, Foreach],
directives: [ScrollAreaComponent, If, For],
inline: `
<div>
<div style="display: flex">
@ -93,7 +93,7 @@ export function setupReflectorForApp() {
</div>
<div template="if scrollAreas.length > 0">
<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>`
})]

View File

@ -7,7 +7,7 @@ import {PromiseWrapper} from 'angular2/src/facade/async';
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {Company, Opportunity, Offering, Account, CustomDate, STATUS_LIST}
from './common';
import {Foreach} from 'angular2/directives';
import {For} from 'angular2/directives';
export class HasStyle {
style:Map;
@ -163,10 +163,10 @@ export function setupReflectorForCells() {
}
}),
new Template({
directives: [Foreach],
directives: [For],
inline: `
<div [style]="style">
<button template="foreach #stage in stages"
<button template="for #stage of stages"
[disabled]="stage.isDisabled"
[style]="stage.style"
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 {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 {ScrollAreaComponent, setupReflectorForScrollArea} from './scroll_area';
import {ScrollItemComponent, setupReflectorForScrollItem} from './scroll_item';
@ -170,13 +170,13 @@ export function setupReflectorForAngular() {
})]
});
reflector.registerType(Foreach, {
'factory': (vp) => new Foreach(vp),
reflector.registerType(For, {
'factory': (vp) => new For(vp),
'parameters': [[ViewContainer]],
'annotations' : [new Viewport({
selector: '[foreach]',
selector: '[for]',
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';
import {generateOfferings} from './random_data';
import {ScrollItemComponent} from './scroll_item';
import {Foreach} from 'angular2/directives';
import {For} from 'angular2/directives';
export class ScrollAreaComponent {
_fullList:List<Offering>;
@ -69,7 +69,7 @@ export function setupReflectorForScrollArea() {
selector: 'scroll-area',
}),
new Template({
directives: [ScrollItemComponent, Foreach],
directives: [ScrollItemComponent, For],
inline: `
<div>
<div id="scrollDiv"
@ -78,7 +78,7 @@ export function setupReflectorForScrollArea() {
<div id="padding"></div>
<div id="inner">
<scroll-item
template="foreach #item in visibleItems"
template="for #item of visibleItems"
[offering]="item">
</scroll-item>
</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';
@Component({
@ -10,7 +10,7 @@ import {Store, Todo, TodoFactory} from './services/TodoStore';
})
@Template({
url: 'todo.html',
directives: [Foreach]
directives: [For]
})
class TodoApp {
todoStore: Store;

View File

@ -18,7 +18,7 @@
<ul id="todo-list">
<li *foreach="#todo in todoStore.list">
<li *for="#todo of todoStore.list">
<div class="view"
[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.
const DART_RESERVED_WORDS = ['if', 'switch'];
const DART_RESERVED_WORDS = ['if', 'switch', 'for'];