Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

feat(ScopeAware): Introduce ScopeAware abstract class. #1360

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,36 @@ class ScopeLocals implements Map {
dynamic putIfAbsent(key, fn) => _scope.putIfAbsent(key, fn);
}

/**
* When a [Component] or the root context class implements [ScopeAware] the scope setter will be
* called to set the [Scope] on this component.
*
* Typically classes implementing [ScopeAware] will declare a `Scope scope` property which will get
* initialized after the [Scope] is available. For this reason the `scope` property will not be
* initialized during the execution of the constructor - it will be immediately after.
*
* However, if you need to execute some code as soon as the scope is available you should implement
* a `scope` setter:
*
* @Component(...)
* class MyComponent implements ScopeAware {
* Watch watch;
*
* MyComponent(Dependency myDep) {
* // It is an error to add a Scope / RootScope argument to the ctor and will result in a DI
* // circular dependency error - the scope is never accessible in the class constructor
* }
*
* void set scope(Scope scope) {
* // This setter gets called to initialize the scope
* watch = scope.rootScope.watch("expression", (v, p) => ...);
* }
* }
*/
abstract class ScopeAware {
void set scope(Scope scope);
}

/**
* [Scope] represents a collection of [watch]es [observer]s, and a [context] for the watchers,
* observers and [eval]uations. Scopes structure loosely mimics the DOM structure. Scopes and
Expand Down
1 change: 1 addition & 0 deletions lib/core_dom/shadow_dom_component_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
}

var controller = shadowInjector.getByKey(_ref.typeKey);
if (controller is ScopeAware) controller.scope = shadowScope;
BoundComponentFactory._setupOnShadowDomAttach(controller, templateLoader, shadowScope);
shadowScope.context[_component.publishAs] = controller;

Expand Down
1 change: 1 addition & 0 deletions lib/core_dom/transcluding_component_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {

var controller = childInjector.getByKey(_ref.typeKey);
shadowScope.context[component.publishAs] = controller;
if (controller is ScopeAware) controller.scope = shadowScope;
BoundComponentFactory._setupOnShadowDomAttach(controller, templateLoader, shadowScope);
return controller;
};
Expand Down
1 change: 1 addition & 0 deletions test/angular_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ main() {
"angular.core_internal.Interpolate",
"angular.core_internal.RootScope",
"angular.core_internal.Scope",
"angular.core_internal.ScopeAware",
"angular.core_internal.ScopeDigestTTL",
"angular.core_internal.ScopeEvent",
"angular.core_internal.ScopeStats",
Expand Down
22 changes: 21 additions & 1 deletion test/core_dom/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ void main() {
..bind(MyChildController)
..bind(MyScopeModifyingController)
..bind(SameNameDecorator)
..bind(SameNameTransclude);
..bind(SameNameTransclude)
..bind(ScopeAwareComponent);
});

beforeEach((TestBed tb) => _ = tb);
Expand Down Expand Up @@ -928,6 +929,14 @@ void main() {

expect(element.text).toContain('my data');
});

it('should call scope setter on ScopeAware components', async((TestBed _, Logger log) {
var element = _.compile('<scope-aware-cmp></scope-aware-cmp>');

_.rootScope.apply();

expect(log.result()).toEqual('Scope set');
}));
});


Expand Down Expand Up @@ -1392,3 +1401,14 @@ class SameNameDecorator {
scope.context['sameDecorator'] = this;
}
}

@Component(
selector: 'scope-aware-cmp'
)
class ScopeAwareComponent implements ScopeAware {
Logger log;
ScopeAwareComponent(this.log) {}
void set scope(Scope scope) {
log('Scope set');
}
}