Skip to content

Commit

Permalink
amp-bind: Add function to print bind's scope (ampproject#8876)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmh287 authored and Eric Kenney committed May 3, 2017
1 parent d22f172 commit 3b4e657
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 21 additions & 2 deletions extensions/amp-bind/0.1/bind-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {dev, user} from '../../../src/log';
import {deepMerge} from '../../../src/utils/object';
import {getMode} from '../../../src/mode';
import {formOrNullForElement} from '../../../src/form';
import {isArray, toArray} from '../../../src/types';
import {isArray, isObject, toArray} from '../../../src/types';
import {isExperimentOn} from '../../../src/experiments';
import {invokeWebWorker} from '../../../src/web-worker/amp-worker';
import {isFiniteNumber} from '../../../src/types';
Expand Down Expand Up @@ -139,7 +139,7 @@ export class Bind {

// Expose for testing on dev.
if (getMode().localDev) {
AMP.reinitializeBind = this.initialize_.bind(this);
AMP.printState = this.printState_.bind(this);
}
}

Expand Down Expand Up @@ -943,6 +943,25 @@ export class Bind {
return false;
}

/**
* Print out the current state in the console.
* @private
*/
printState_() {
const seen = [];
const s = JSON.stringify(this.scope_, (key, value) => {
if (isObject(value)) {
if (seen.includes(value)) {
return '[Circular]';
} else {
seen.push(value);
}
}
return value;
});
dev().info(TAG, s);
}

/**
* Wait for bind scan to finish for testing.
*
Expand Down
8 changes: 6 additions & 2 deletions extensions/amp-bind/amp-bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ Only binding to the following components and attributes are allowed:

## Debugging

Test in development mode (with the URL fragment `#development=1`) to highlight warnings and errors during development.
Test in development mode (with the URL fragment `#development=1`) to highlight warnings and errors during development and to access special debugging functions.

### Warnings

Expand Down Expand Up @@ -471,6 +471,10 @@ There are several types of runtime errors that may be encountered when working w
</tr>
</table>

### Debugging State

In development mode, use `AMP.printState()` to print the current state to the console.

## Appendix

### `<amp-state>` specification
Expand All @@ -489,7 +493,7 @@ An `amp-state` element may contain either a child `<script>` element **OR** a `s
<amp-state id="myRemoteState" src="https://data.com/articles.json">
</amp-state>
```
<br>

#### Attributes

**src**
Expand Down

0 comments on commit 3b4e657

Please sign in to comment.