X Tutup
Skip to content

Commit 1746130

Browse files
committed
feat(views): adds (de)hydration of views and template vars.
Dehydrated views are views that are structurally fixed, but their directive instances and viewports are purged. Support for local bindings is added to the view.
1 parent 5c531f7 commit 1746130

File tree

11 files changed

+413
-109
lines changed

11 files changed

+413
-109
lines changed

modules/change_detection/src/parser/context_with_variable_bindings.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {MapWrapper} from 'facade/collection';
2+
import {BaseException} from 'facade/lang';
23

34
export class ContextWithVariableBindings {
45
parent:any;
5-
/// varBindings are read-only. updating/adding keys is not supported.
6+
/// varBindings' keys are read-only. adding/removing keys is not supported.
67
varBindings:Map;
78

89
constructor(parent:any, varBindings:Map) {
@@ -17,4 +18,21 @@ export class ContextWithVariableBindings {
1718
get(name:string) {
1819
return MapWrapper.get(this.varBindings, name);
1920
}
20-
}
21+
22+
set(name:string, value) {
23+
// TODO(rado): consider removing this check if we can guarantee this is not
24+
// exposed to the public API.
25+
if (this.hasBinding(name)) {
26+
MapWrapper.set(this.varBindings, name, value);
27+
} else {
28+
throw new BaseException(
29+
'VariableBindings do not support setting of new keys post-construction.');
30+
}
31+
}
32+
33+
clearValues() {
34+
for (var [k, v] of MapWrapper.iterable(this.varBindings)) {
35+
MapWrapper.set(this.varBindings, k, null);
36+
}
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {ddescribe, describe, it, xit, iit, expect, beforeEach} from 'test_lib/test_lib';
2+
import {BaseException, isBlank, isPresent} from 'facade/lang';
3+
import {MapWrapper, ListWrapper} from 'facade/collection';
4+
import {ContextWithVariableBindings} from 'change_detection/parser/context_with_variable_bindings';
5+
6+
export function main() {
7+
describe('ContextWithVariableBindings', () => {
8+
var locals;
9+
beforeEach(() => {
10+
locals = new ContextWithVariableBindings(null,
11+
MapWrapper.createFromPairs([['key', 'value'], ['nullKey', null]]));
12+
});
13+
14+
it('should support getting values', () => {
15+
expect(locals.get('key')).toBe('value');
16+
17+
var notPresentValue = locals.get('notPresent');
18+
expect(isPresent(notPresentValue)).toBe(false);
19+
});
20+
21+
it('should support checking if key is persent', () => {
22+
expect(locals.hasBinding('key')).toBe(true);
23+
expect(locals.hasBinding('nullKey')).toBe(true);
24+
expect(locals.hasBinding('notPresent')).toBe(false);
25+
});
26+
27+
it('should support setting persent keys', () => {
28+
locals.set('key', 'bar');
29+
expect(locals.get('key')).toBe('bar');
30+
});
31+
32+
it('should not support setting keys that are not present already', () => {
33+
expect(() => locals.set('notPresent', 'bar')).toThrowError();
34+
});
35+
36+
it('should clearValues', () => {
37+
locals.clearValues();
38+
expect(locals.get('key')).toBe(null);
39+
});
40+
})
41+
}
42+

modules/core/src/application.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export function documentDependentBindings(appComponentType) {
5454
// The light Dom of the app element is not considered part of
5555
// the angular application. Thus the context and lightDomInjector are
5656
// empty.
57-
return appProtoView.instantiate(new Object(), injector, null, true);
57+
var view = appProtoView.instantiate(null, true);
58+
view.hydrate(injector, null, new Object());
59+
return view;
5860
});
5961
}, [Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken]),
6062

modules/core/src/compiler/element_injector.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ export class ElementInjector extends TreeNode {
459459
if (index == 9) return this._obj9;
460460
throw new OutOfBoundsAccess(index);
461461
}
462+
463+
hasInstances() {
464+
return this._constructionCounter > 0;
465+
}
462466
}
463467

464468
class OutOfBoundsAccess extends Error {

0 commit comments

Comments
 (0)
X Tutup