diff --git a/modules/angular2/src/core/compiler/query_list.ts b/modules/angular2/src/core/compiler/query_list.ts index 5915e86d05..4b4f3315c4 100644 --- a/modules/angular2/src/core/compiler/query_list.ts +++ b/modules/angular2/src/core/compiler/query_list.ts @@ -1,4 +1,6 @@ import {ListWrapper, MapWrapper} from 'angular2/src/core/facade/collection'; +import {getSymbolIterator} from 'angular2/src/core/facade/lang'; + /** * An iterable and observable live list of components in the DOM. @@ -101,7 +103,7 @@ export class QueryList { map(fn: (item: T) => U): U[] { return this._results.map(fn); } - [Symbol.iterator](): any { return this._results[Symbol.iterator](); } + [getSymbolIterator()](): any { return this._results[getSymbolIterator()](); } // Internal to the framework. fireCallbacks(): void { diff --git a/modules/angular2/src/core/facade/collection.ts b/modules/angular2/src/core/facade/collection.ts index cb75864506..31e8c46a3d 100644 --- a/modules/angular2/src/core/facade/collection.ts +++ b/modules/angular2/src/core/facade/collection.ts @@ -1,4 +1,11 @@ -import {isJsObject, global, isPresent, isBlank, isArray} from 'angular2/src/core/facade/lang'; +import { + isJsObject, + global, + isPresent, + isBlank, + isArray, + getSymbolIterator +} from 'angular2/src/core/facade/lang'; export var Map = global.Map; export var Set = global.Set; @@ -289,8 +296,8 @@ export class ListWrapper { export function isListLikeIterable(obj: any): boolean { if (!isJsObject(obj)) return false; return isArray(obj) || - (!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v] - Symbol.iterator in obj); // JS Iterable have a Symbol.iterator prop + (!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v] + getSymbolIterator() in obj); // JS Iterable have a Symbol.iterator prop } export function iterateListLike(obj: any, fn: Function) { @@ -299,7 +306,7 @@ export function iterateListLike(obj: any, fn: Function) { fn(obj[i]); } } else { - var iterator = obj[Symbol.iterator](); + var iterator = obj[getSymbolIterator()](); var item; while (!((item = iterator.next()).done)) { fn(item.value); diff --git a/modules/angular2/src/core/facade/lang.ts b/modules/angular2/src/core/facade/lang.ts index dc84b854df..487e1bd39e 100644 --- a/modules/angular2/src/core/facade/lang.ts +++ b/modules/angular2/src/core/facade/lang.ts @@ -351,3 +351,24 @@ export function setValueOnPath(global: any, path: string, value: any) { } obj[parts.shift()] = value; } + +// When Symbol.iterator doesn't exist, retrieves the key used in es6-shim +var _symbolIterator = null; +export function getSymbolIterator(): string | symbol { + if (isBlank(_symbolIterator)) { + if (isPresent(Symbol) && isPresent(Symbol.iterator)) { + _symbolIterator = Symbol.iterator; + } else { + // es6-shim specific logic + var keys = Object.getOwnPropertyNames(Map.prototype); + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + if (key !== 'entries' && key !== 'size' && + Map.prototype[key] === Map.prototype['entries']) { + _symbolIterator = key; + } + } + } + } + return _symbolIterator; +} diff --git a/modules/angular2/test/core/change_detection/iterable.ts b/modules/angular2/test/core/change_detection/iterable.ts index e44c4ab6f6..59014795a3 100644 --- a/modules/angular2/test/core/change_detection/iterable.ts +++ b/modules/angular2/test/core/change_detection/iterable.ts @@ -1,7 +1,8 @@ +import {getSymbolIterator} from 'angular2/src/core/facade/lang'; export class TestIterable { list: number[]; constructor() { this.list = []; } - [Symbol.iterator]() { return this.list[Symbol.iterator](); } + [getSymbolIterator()]() { return this.list[getSymbolIterator()](); } }