diff --git a/modules/angular2/src/compiler/identifiers.ts b/modules/angular2/src/compiler/identifiers.ts index 3938001b22..efa5ed16bf 100644 --- a/modules/angular2/src/compiler/identifiers.ts +++ b/modules/angular2/src/compiler/identifiers.ts @@ -7,6 +7,8 @@ import { interpolate, checkBinding, castByValue, + EMPTY_ARRAY, + EMPTY_MAP, pureProxy1, pureProxy2, pureProxy3, @@ -72,6 +74,8 @@ var impDevModeEqual = devModeEqual; var impInterpolate = interpolate; var impCheckBinding = checkBinding; var impCastByValue = castByValue; +var impEMPTY_ARRAY = EMPTY_ARRAY; +var impEMPTY_MAP = EMPTY_MAP; export class Identifiers { static ViewUtils = new CompileIdentifierMetadata({ @@ -179,6 +183,11 @@ export class Identifiers { {name: 'interpolate', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impInterpolate}); static castByValue = new CompileIdentifierMetadata( {name: 'castByValue', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impCastByValue}); + static EMPTY_ARRAY = new CompileIdentifierMetadata( + {name: 'EMPTY_ARRAY', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impEMPTY_ARRAY}); + static EMPTY_MAP = new CompileIdentifierMetadata( + {name: 'EMPTY_MAP', moduleUrl: VIEW_UTILS_MODULE_URL, runtime: impEMPTY_MAP}); + static pureProxies = [ null, new CompileIdentifierMetadata( diff --git a/modules/angular2/src/compiler/view_compiler/compile_view.ts b/modules/angular2/src/compiler/view_compiler/compile_view.ts index 6ead94a539..7b28d30c54 100644 --- a/modules/angular2/src/compiler/view_compiler/compile_view.ts +++ b/modules/angular2/src/compiler/view_compiler/compile_view.ts @@ -24,6 +24,7 @@ import { } from './util'; import {CompilerConfig} from '../config'; import {CompileBinding} from './compile_binding'; +import {Identifiers} from '../identifiers'; export class CompileView implements NameResolver { public viewType: ViewType; @@ -155,6 +156,9 @@ export class CompileView implements NameResolver { } createLiteralArray(values: o.Expression[]): o.Expression { + if (values.length === 0) { + return o.importExpr(Identifiers.EMPTY_ARRAY); + } var proxyExpr = o.THIS_EXPR.prop(`_arr_${this.literalArrayCount++}`); var proxyParams: o.FnParam[] = []; var proxyReturnEntries: o.Expression[] = []; @@ -169,6 +173,9 @@ export class CompileView implements NameResolver { } createLiteralMap(entries: Array>): o.Expression { + if (entries.length === 0) { + return o.importExpr(Identifiers.EMPTY_MAP); + } var proxyExpr = o.THIS_EXPR.prop(`_map_${this.literalMapCount++}`); var proxyParams: o.FnParam[] = []; var proxyReturnEntries: Array> = []; diff --git a/modules/angular2/src/core/linker/view_utils.ts b/modules/angular2/src/core/linker/view_utils.ts index c0d87039fe..93b8b82f5a 100644 --- a/modules/angular2/src/core/linker/view_utils.ts +++ b/modules/angular2/src/core/linker/view_utils.ts @@ -156,6 +156,9 @@ export function castByValue(input: any, value: T): T { return input; } +export const EMPTY_ARRAY = CONST_EXPR([]); +export const EMPTY_MAP = CONST_EXPR({}); + export function pureProxy1(fn: (p0: P0) => R): (p0: P0) => R { var result: R; var v0; diff --git a/modules/angular2/test/core/linker/change_detection_integration_spec.ts b/modules/angular2/test/core/linker/change_detection_integration_spec.ts index 1ce2073b11..8dc5aaad2a 100644 --- a/modules/angular2/test/core/linker/change_detection_integration_spec.ts +++ b/modules/angular2/test/core/linker/change_detection_integration_spec.ts @@ -371,6 +371,12 @@ export function main() { expect(renderLog.loggedValues).toEqual([[1, 2]]); })); + it('should support empty literal array', fakeAsync(() => { + var ctx = _bindSimpleValue('[]'); + ctx.detectChanges(false); + expect(renderLog.loggedValues).toEqual([[]]); + })); + it('should support literal array made of expressions', fakeAsync(() => { var ctx = _bindSimpleValue('[1, a]', TestData); ctx.componentInstance.a = 2; @@ -395,6 +401,12 @@ export function main() { expect(renderLog.loggedValues[0]['z']).toEqual(1); })); + it('should support empty literal map', fakeAsync(() => { + var ctx = _bindSimpleValue('{}'); + ctx.detectChanges(false); + expect(renderLog.loggedValues).toEqual([{}]); + })); + it('should support literal maps made of expressions', fakeAsync(() => { var ctx = _bindSimpleValue('{z: a}'); ctx.componentInstance.a = 1;