Skip to content

Commit 0f61c5e

Browse files
matskojbdeboer
authored andcommitted
docs(Scope): complete missing documentation for scope methods/properties
Closes dart-archive#287
1 parent 50ab179 commit 0f61c5e

File tree

1 file changed

+142
-1
lines changed

1 file changed

+142
-1
lines changed

lib/core/scope.dart

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,50 @@ part of angular.core;
22

33

44
/**
5-
* Used by [Scope.$on] to notify the listeners of events.
5+
* Injected into the listener function within [Scope.$on] to provide event-specific
6+
* details to the scope listener.
67
*/
78
class ScopeEvent {
9+
10+
/**
11+
* The name of the intercepted scope event.
12+
*/
813
String name;
14+
15+
/**
16+
* The origin scope that triggered the event (via $broadcast or $emit).
17+
*/
918
Scope targetScope;
19+
20+
/**
21+
* The destination scope that intercepted the event.
22+
*/
1023
Scope currentScope;
24+
25+
/**
26+
* true or false depending on if stopPropagation() was executed.
27+
*/
1128
bool propagationStopped = false;
29+
30+
/**
31+
* true or false depending on if preventDefault() was executed.
32+
*/
1233
bool defaultPrevented = false;
1334

35+
/**
36+
** [name] - The name of the scope event.
37+
** [targetScope] - The destination scope that is listening on the event.
38+
*/
1439
ScopeEvent(this.name, this.targetScope);
1540

41+
/**
42+
* Prevents the intercepted event from propagating further to successive scopes.
43+
*/
1644
stopPropagation () => propagationStopped = true;
45+
46+
/**
47+
* Sets the defaultPrevented flag to true.
48+
*/
1749
preventDefault() => defaultPrevented = true;
1850
}
1951

@@ -50,9 +82,20 @@ class Scope implements Map {
5082
final bool _isolate;
5183
final bool _lazy;
5284
final Profiler _perf;
85+
86+
/**
87+
* The direct parent scope that created this scope (this can also be the $rootScope)
88+
*/
5389
final Scope $parent;
5490

91+
/**
92+
* The auto-incremented ID of the scope
93+
*/
5594
String $id;
95+
96+
/**
97+
* The topmost scope of the application (same as $rootScope).
98+
*/
5699
Scope $root;
57100
num _nextId = 0;
58101
String _phase;
@@ -409,6 +452,21 @@ class Scope implements Map {
409452
this._disabled = false;
410453
}
411454

455+
/**
456+
* Processes all of the watchers of the current scope and its children.
457+
* Because a watcher's listener can change the model, the `$digest()` operation keeps calling
458+
* the watchers no further response data has changed. This means that it is possible to get
459+
* into an infinite loop. This function will throw `'Maximum iteration limit exceeded.'`
460+
* if the number of iterations exceeds 10.
461+
*
462+
* There should really be no need to call $digest() in production code since everything is
463+
* handled behind the scenes with zones and object mutation events. However, in testing
464+
* both $digest and [$apply] are useful to control state and simulate the scope life cycle in
465+
* a step-by-step manner.
466+
*
467+
* Refer to [$watch], [$watchSet] or [$watchCollection] to see how to register watchers that
468+
* are executed during the digest cycle.
469+
*/
412470
$digest() {
413471
try {
414472
_beginPhase('\$digest');
@@ -600,6 +658,21 @@ class Scope implements Map {
600658
}
601659

602660

661+
/**
662+
* Removes the current scope (and all of its children) from the parent scope. Removal implies
663+
* that calls to $digest() will no longer propagate to the current scope and its children.
664+
* Removal also implies that the current scope is eligible for garbage collection.
665+
*
666+
* The `$destroy()` operation is usually used within directives that perform transclusion on
667+
* multiple child elements (like ngRepeat) which create multiple child scopes.
668+
*
669+
* Just before a scope is destroyed, a `$destroy` event is broadcasted on this scope. This is
670+
* a great way for child scopes (such as shared directives or controllers) to detect to and
671+
* perform any necessary cleanup before the scope is removed from the application.
672+
*
673+
* Note that, in AngularDart, there is also a `$destroy` jQuery DOM event, which can be used to
674+
* clean up DOM bindings before an element is removed from the DOM.
675+
*/
603676
$destroy() {
604677
if ($root == this) return; // we can't remove the root node;
605678

@@ -612,11 +685,32 @@ class Scope implements Map {
612685
}
613686

614687

688+
/**
689+
* Evaluates the expression against the current scope and returns the result. Note that, the
690+
* expression data is relative to the data within the scope. Therefore an expression such as
691+
* `a + b` will deference variables `a` and `b` and return a result so long as `a` and `b`
692+
* exist on the scope.
693+
*
694+
* * [expr] - The expression that will be evaluated. This can be both a Function or a String.
695+
* * [locals] - An optional Map of key/value data that will override any matching scope members
696+
* for the purposes of the evaluation.
697+
*/
615698
$eval(expr, [locals]) {
616699
return relaxFnArgs(_compileToFn(expr))(locals == null ? this : new ScopeLocals(this, locals));
617700
}
618701

619702

703+
/**
704+
* Evaluates the expression against the current scope at a later point in time. The $evalAsync
705+
* operation may not get run right away (depending if an existing digest cycle is going on) and
706+
* may therefore be issued later on (by a follow-up digest cycle). Note that at least one digest
707+
* cycle will be performed after the expression is evaluated. However, If triggering an additional
708+
* digest cycle is not desired then this can be avoided by placing `{outsideDigest: true}` as
709+
* the 2nd parameter to the function.
710+
*
711+
* * [expr] - The expression that will be evaluated. This can be both a Function or a String.
712+
* * [outsideDigest] - Whether or not to trigger a follow-up digest after evaluation.
713+
*/
620714
$evalAsync(expr, {outsideDigest: false}) {
621715
if (outsideDigest) {
622716
_outerAsyncQueue.add(expr);
@@ -651,6 +745,16 @@ class Scope implements Map {
651745
}
652746

653747

748+
/**
749+
* Triggers a digest operation much like [$digest] does, however, also accepts an
750+
* optional expression to evaluate alongside the digest operation. The result of that
751+
* expression will be returned afterwards. Much like with $digest, $apply should only be
752+
* used within unit tests to simulate the life cycle of a scope. See [$digest] to learn
753+
* more.
754+
*
755+
* * [expr] - optional expression which will be evaluated after the digest is performed. See [$eval]
756+
* to learn more about expressions.
757+
*/
654758
$apply([expr]) {
655759
return _zone.run(() {
656760
var timerId;
@@ -666,6 +770,23 @@ class Scope implements Map {
666770
}
667771

668772

773+
/**
774+
* Registers a scope-based event listener to intercept events triggered by
775+
* [$broadcast] (from any parent scopes) or [$emit] (from child scopes) that
776+
* match the given event name. $on accepts two arguments:
777+
*
778+
* * [name] - Refers to the event name that the scope will listen on.
779+
* * [listener] - Refers to the callback function which is executed when the event
780+
* is intercepted.
781+
*
782+
*
783+
* When the listener function is executed, an instance of [ScopeEvent] will be passed
784+
* as the first parameter to the function.
785+
*
786+
* Any additional parameters available within the listener callback function are those that
787+
* are set by the $broadcast or $emit scope methods (which are set by the origin scope which
788+
* is the scope that first triggered the scope event).
789+
*/
669790
$on(name, listener) {
670791
var namedListeners = _listeners[name];
671792
if (!_listeners.containsKey(name)) {
@@ -679,6 +800,16 @@ class Scope implements Map {
679800
}
680801

681802

803+
/**
804+
* Triggers a scope event referenced by the [name] parameters upwards towards the root of the
805+
* scope tree. If intercepted, by a parent scope containing a matching scope event listener
806+
* (which is registered via the [$on] scope method), then the event listener callback function
807+
* will be executed.
808+
*
809+
* * [name] - The scope event name that will be triggered.
810+
* * [args] - An optional list of arguments that will be fed into the listener callback function
811+
* for any event listeners that are registered via [$on].
812+
*/
682813
$emit(name, [List args]) {
683814
var empty = [],
684815
namedListeners,
@@ -713,6 +844,16 @@ class Scope implements Map {
713844
}
714845

715846

847+
/**
848+
* Triggers a scope event referenced by the [name] parameters dowards towards the leaf nodes of the
849+
* scope tree. If intercepted, by a child scope containing a matching scope event listener
850+
* (which is registered via the [$on] scope method), then the event listener callback function
851+
* will be executed.
852+
*
853+
* * [name] - The scope event name that will be triggered.
854+
* * [listenerArgs] - An optional list of arguments that will be fed into the listener callback function
855+
* for any event listeners that are registered via [$on].
856+
*/
716857
$broadcast(String name, [List listenerArgs]) {
717858
var target = this,
718859
current = target,

0 commit comments

Comments
 (0)