Skip to content

Commit 2681820

Browse files
vicbrkirov
authored andcommitted
feat(scope): component is the new context
BREAKING CHANGE: Scope context is set to the component instance that trigged the creation of the scope (previously it was of a PrototypeMap.) Repercussions: 1) You can not inject a scope in a component or in the root context any more. As the Scope context is set to the Component instance, the scope could not be injected any more. Components should implements the "ScopeAware" interface and declare a "scope" setter in order to get a reference to the scope. before: @component(...) class MyComponent { Watch watch; Scope scope; MyComponent(Dependency myDep, Scope scope) { watch = scope.rootScope.watch("expression", (v, p) => ...); } } after: @component(...) class MyComponent implements ScopeAware { Watch watch; MyComponent(Dependency myDep) { // It is an error to add a Scope 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.watch("expression", (v, p) => ...); } } or: @component(...) class MyComponent implements ScopeAware { Scope scope; MyComponent(Dependency myDep) { // It is an error to add a Scope argument to the // ctor and will result in a DI circular dependency error // The scope is never accessible in the class constructor } } 2) The parent component to an NgForm must have a "$name" field to store the form instance. closes dart-archive#919 closes dart-archive#917
1 parent a893b30 commit 2681820

File tree

71 files changed

+4364
-665
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4364
-665
lines changed

benchmark/web/tree.dart

+7-12
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import 'package:angular/application_factory.dart';
55
import 'package:angular/change_detection/ast_parser.dart';
66

77
import 'dart:html';
8-
import 'dart:math';
98
import 'dart:js' as js;
109

1110
@Component(
1211
selector: 'tree',
1312
template: '<span> {{ctrl.data.value}}'
14-
'<span ng-if="ctrl.data.right != null"><tree data=ctrl.data.right></span>'
15-
'<span ng-if="ctrl.data.left != null"><tree data=ctrl.data.left></span>'
16-
'</span>',
17-
publishAs: 'ctrl',
18-
useShadowDom: true)
13+
'<span ng-if="data.right != null"><tree data=data.right></span>'
14+
'<span ng-if="data.left != null"><tree data=data.left></span>'
15+
'</span>')
1916
class TreeComponent {
2017
@NgOneWay('data')
2118
var data;
@@ -34,9 +31,7 @@ class TranscludingTreeComponent extends TreeComponent {}
3431

3532
@Component(
3633
selector: 'tree-url',
37-
templateUrl: 'tree-tmpl.html',
38-
publishAs: 'ctrl',
39-
useShadowDom: true)
34+
templateUrl: 'tree-tmpl.html')
4035
class TreeUrlComponent {
4136
@NgOneWay('data')
4237
var data;
@@ -209,19 +204,19 @@ class FreeTreeClass {
209204
s.text = " $v";
210205
}
211206
});
212-
207+
213208
scope.watchAST(treeRightNotNullAST, (v, _) {
214209
if (v != true) return;
215210
s.append(new SpanElement()
216211
..append(new FreeTreeClass(scope, treeRightAST).element()));
217212
});
218-
213+
219214
scope.watchAST(treeLeftNotNullAST, (v, _) {
220215
if (v != true) return;
221216
s.append(new SpanElement()
222217
..append(new FreeTreeClass(scope, treeLeftAST).element()));
223218
});
224-
219+
225220
return elt;
226221
}
227222
}

example/pubspec.lock

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ packages:
1818
barback:
1919
description: barback
2020
source: hosted
21-
version: "0.14.0+3"
21+
version: "0.14.2"
2222
browser:
2323
description: browser
2424
source: hosted
@@ -59,6 +59,10 @@ packages:
5959
description: perf_api
6060
source: hosted
6161
version: "0.0.9"
62+
pool:
63+
description: pool
64+
source: hosted
65+
version: "1.0.1"
6266
quiver:
6367
description: quiver
6468
source: hosted
@@ -78,7 +82,7 @@ packages:
7882
stack_trace:
7983
description: stack_trace
8084
source: hosted
81-
version: "0.9.3+2"
85+
version: "1.0.2"
8286
typed_mock:
8387
description: typed_mock
8488
source: hosted

example/pubspec.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ dependencies:
55
path: ../
66
browser: any
77
unittest: any
8-
quiver: any
98
web_components: any
109

1110
transformers:

example/web/animation.dart

+1-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ library animation;
33
import 'package:angular/angular.dart';
44
import 'package:angular/application_factory.dart';
55
import 'package:angular/animate/module.dart';
6-
import 'package:quiver/collection.dart';
76

87
part 'animation/repeat_demo.dart';
98
part 'animation/visibility_demo.dart';
@@ -16,29 +15,18 @@ class AnimationDemo {
1615
var currentPage = "About";
1716
}
1817

19-
// Temporary workaround, because context needs to extend Map.
20-
@Injectable()
21-
class AnimationDemoHashMap extends DelegatingMap {
22-
final Map _delegate;
23-
AnimationDemoHashMap(AnimationDemo demo) : _delegate = new Map() {
24-
_delegate['demo'] = demo;
25-
}
26-
Map get delegate => _delegate;
27-
}
28-
2918
class AnimationDemoModule extends Module {
3019
AnimationDemoModule() {
3120
install(new AnimationModule());
3221
bind(RepeatDemo);
3322
bind(VisibilityDemo);
3423
bind(StressDemo);
3524
bind(CssDemo);
36-
bind(AnimationDemo);
3725
}
3826
}
3927
main() {
4028
applicationFactory()
4129
.addModule(new AnimationDemoModule())
42-
.rootContextType(AnimationDemoHashMap)
30+
.rootContextType(AnimationDemo)
4331
.run();
4432
}

example/web/animation.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
</head>
77
<body class="ng-cloak" ng-app animation-demo>
88
<nav>
9-
<button ng-repeat="page in demo.pages"
10-
ng-click="demo.currentPage = page"
11-
ng-class="{'current': demo.currentPage == page}">
9+
<button ng-repeat="page in pages"
10+
ng-click="currentPage = page"
11+
ng-class="{'current': currentPage == page}">
1212
{{page}}
1313
</button>
1414
</nav>
15-
<div class="content" ng-switch="demo.currentPage">
15+
<div class="content" ng-switch="currentPage">
1616
<div class="demo" ng-switch-default>
1717

1818
<h2>About</h2>

example/web/animation/css_demo.dart

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@ part of animation;
44
selector: 'css-demo',
55
template: '''
66
<div class="css-demo">
7-
<button ng-click="ctrl.stateA = !ctrl.stateA"
8-
ng-class="{'active': ctrl.stateA}">
7+
<button ng-click="stateA = !stateA"
8+
ng-class="{'active': stateA}">
99
Toggle A</button>
10-
<button ng-click="ctrl.stateB = !ctrl.stateB"
11-
ng-class="{'active': ctrl.stateB}">
10+
<button ng-click="stateB = !stateB"
11+
ng-class="{'active': stateB}">
1212
Toggle B</button>
13-
<button ng-click="ctrl.stateC = !ctrl.stateC"
14-
ng-class="{'active': ctrl.stateC}">
13+
<button ng-click="stateC = !stateC"
14+
ng-class="{'active': stateC}">
1515
Toggle C</button>
1616
<div class="box-container">
1717
<div class="css-box" ng-class="{
18-
'a': ctrl.stateA,
19-
'b': ctrl.stateB,
20-
'c': ctrl.stateC}">BOX</div>
18+
'a': stateA,
19+
'b': stateB,
20+
'c': stateC}">BOX</div>
2121
</div>
2222
</div>
2323
</div>
24-
''',
25-
publishAs: 'ctrl')
24+
''')
2625
class CssDemo {
2726
bool stateA = false;
2827
bool stateB = false;

example/web/animation/repeat_demo.dart

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ part of animation;
55
useShadowDom: false,
66
template: '''
77
<div class="repeat-demo">
8-
<button ng-click="ctrl.addItem()">Add Thing</button>
9-
<button ng-click="ctrl.removeItem()">Remove Thing
10-
</button>
8+
<button ng-click="addItem()">Add Thing</button>
9+
<button ng-click="removeItem()">Remove Thing</button>
1110
<ul>
12-
<li ng-repeat="outer in ctrl.items">
11+
<li ng-repeat="outer in items">
1312
<ul>
14-
<li ng-repeat="inner in ctrl.items">{{inner}}</li>
13+
<li ng-repeat="inner in items">{{inner}}</li>
1514
</ul>
1615
</li>
1716
</ul>
1817
</div>
19-
''',
20-
publishAs: 'ctrl')
18+
''')
2119
class RepeatDemo {
2220
var thing = 0;
2321
final items = [];

example/web/animation/stress_demo.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ part of animation;
44
selector: 'stress-demo',
55
template: '''
66
<div class="stress-demo">
7-
<button ng-click="ctrl.visible = !ctrl.visible">
7+
<button ng-click="visible = !visible">
88
Toggle Visibility</button>
99
<div>
10-
<div class="stress-box" ng-repeat="number in ctrl.numbers"></div>
10+
<div class="stress-box" ng-repeat="number in numbers"></div>
1111
</div>
1212
</div>
13-
''',
14-
publishAs: 'ctrl')
13+
''')
1514
class StressDemo {
1615
bool _visible = true;
1716
final numbers = <int>[1, 2];

example/web/animation/visibility_demo.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ part of animation;
44
selector: 'visibility-demo',
55
template: '''
66
<div class="visibility-demo">
7-
<button ng-click="ctrl.visible = !ctrl.visible">Toggle Visibility</button>
8-
<div class="visible-if" ng-if="ctrl.visible">
7+
<button ng-click="visible = !visible">Toggle Visibility</button>
8+
<div class="visible-if" ng-if="visible">
99
<p>Hello World. ng-if will create and destroy
1010
dom elements each time you toggle me.</p>
1111
</div>
12-
<div class="visible-hide" ng-hide="ctrl.visible">
12+
<div class="visible-hide" ng-hide="visible">
1313
<p>Hello World. ng-hide will add and remove
1414
the .ng-hide class from me to show and
1515
hide this view of text.</p>
1616
</div>
1717
</div>
1818
''',
19-
publishAs: 'ctrl',
2019
useShadowDom: false)
2120
class VisibilityDemo {
2221
bool visible = false;

example/web/bouncing_balls.dart

+6-12
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ class BallModel {
2626
}
2727
}
2828

29-
30-
@Component(
31-
selector: 'bounce-controller',
32-
publishAs: 'ctrl',
33-
templateUrl: 'bouncing_controller.html',
34-
cssUrl: 'bouncing_controller.css'
35-
)
36-
class BounceController {
37-
RootScope rootScope;
29+
@Injectable()
30+
class BounceController implements ScopeAware {
31+
Scope scope;
3832
var lastTime = window.performance.now();
3933
var run = false;
4034
var fps = 0;
@@ -43,9 +37,8 @@ class BounceController {
4337
var balls = [];
4438
var ballClassName = 'ball';
4539

46-
BounceController(this.rootScope) {
40+
BounceController() {
4741
changeCount(100);
48-
if (run) tick();
4942
}
5043

5144
void toggleCSS() {
@@ -76,7 +69,7 @@ class BounceController {
7669
void timeDigest() {
7770
var start = window.performance.now();
7871
digestTime = currentDigestTime;
79-
rootScope.domRead(() {
72+
scope.rootScope.domRead(() {
8073
currentDigestTime = window.performance.now() - start;
8174
});
8275
}
@@ -135,6 +128,7 @@ class MyModule extends Module {
135128

136129
main() {
137130
applicationFactory()
131+
.rootContextType(BounceController)
138132
.addModule(new MyModule())
139133
.run();
140134
}

example/web/bouncing_balls.html

+60-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,68 @@
22
<html>
33
<head>
44
<title>Bouncing balls</title>
5+
<style>
6+
.balls {
7+
border: 1px solid black;
8+
width: 420px;
9+
height: 420px;
10+
margin: 5px;
11+
}
12+
13+
.ball {
14+
display: inline-block;
15+
position: absolute;
16+
width: 20px;
17+
height: 20px;
18+
border: 1px solid black;
19+
-webkit-border-radius: 10px;
20+
-moz-border-radius: 10px;
21+
border-radius: 10px;
22+
}
23+
24+
.fps-bar {
25+
width: 200px;
26+
height: 10px;
27+
border: 1px solid black;
28+
display: inline-block;
29+
margin-left: 5px;
30+
}
31+
32+
.fps {
33+
height: 10px;
34+
width: 60px;
35+
background-color: green;
36+
}
37+
</style>
538
</head>
639
<body ng-app>
7-
<bounce-controller></bounce-controller>
40+
<div bounce-controller>
41+
<div class="balls">
42+
<div ng-repeat="ball in balls"
43+
class="{{ ballClassName }}"
44+
ball-position="ball"></div>
45+
</div>
46+
47+
<div>
48+
<div class="fps-bar">
49+
<div class="fps" ng-style-width="fps * 4 + 'px'"></div>
50+
</div>
51+
</div>
52+
53+
{{ fps }} fps. ({{ balls.length }} balls) [{{ 1000 / fps }} ms] <br>
54+
Digest: {{ digestTime }} ms<br>
55+
<a href ng-click="changeCount(1)">+1</a>
56+
<a href ng-click="changeCount(10)">+10</a>
57+
<a href ng-click="changeCount(100)">+100</a>
58+
<br>
59+
<a href ng-click="changeCount(-1)">-1</a>
60+
<a href ng-click="changeCount(-10)">-10</a>
61+
<a href ng-click="changeCount(-100)">-100</a>
62+
<br>
63+
<a href ng-click="playPause()">&#x25B6;&#10073;&#10073;</a> <br>
64+
<a href ng-click="toggleCSS()">Toggle CSS</a><br>
65+
<a href ng-click="timeDigest()">noop</a><br>
66+
</div>
867

968
<script type="application/dart" src="bouncing_balls.dart"></script>
1069
<script src="packages/browser/dart.js"></script>

0 commit comments

Comments
 (0)