|
2 | 2 |
|
3 | 3 | The default version of the **API console does not contain the RAML parser**. It is by design to minimize the size of the console and to optimize startup time. |
4 | 4 |
|
5 | | -You can however use the console with the RAML parser setting up the build / CLI tools or by adding the dependency manually. **This document describes why the parser has been removed from the core console code and how to use the parser with new console.** |
| 5 | +You can use the console with the RAML parser by setting up the build / CLI tools or by adding the dependency manually. **This document describes why the parser has been removed from the core console code and how to use the parser with new console.** |
6 | 6 |
|
7 | 7 | ## Performance of the API console |
8 | 8 |
|
@@ -44,7 +44,7 @@ You can install parser and enhancer in your project by calling [bower][9] comman |
44 | 44 | $ bower install --save advanced-rest-client/raml-json-enhance advanced-rest-client/raml-js-parser |
45 | 45 | ``` |
46 | 46 |
|
47 | | -You can use `package.json` script's declaration for the same command: |
| 47 | +You can use `package.json` script declaration for the same command: |
48 | 48 |
|
49 | 49 | ```json |
50 | 50 | "scripts": { |
@@ -77,59 +77,32 @@ Also check out our usage guide of the `<api-console>` element in our [element's |
77 | 77 | <api-console></api-console> |
78 | 78 |
|
79 | 79 | <script> |
80 | | -var parser = document.querySelector('raml-js-parser'); |
81 | | -parser.addEventListener('api-parse-ready', function(e) { |
82 | | - var enhacer = document.querySelector('raml-json-enhance'); |
83 | | - enhacer.json = e.detail.json.specification; |
84 | | -}); |
85 | | -window.addEventListener('raml-json-enhance-ready', function(e) { |
86 | | - // The e.detail.json contains the final JavaScript object |
87 | | - var apiConsole = document.querySelector('api-console'); |
88 | | - apiConsole.raml = e.detail.json; |
89 | | -}); |
90 | | -// Assuming components are already loaded and registered |
91 | | -parser.loadApi('https://domain.com/api.raml'); |
92 | | -</script> |
93 | | -``` |
94 | | - |
95 | | -The `json` attribute set on `raml-js-parser` element tells the parser that the output should be a JavaScript object instead of the AST. |
96 | | - |
97 | | -## Asynchronous environment |
98 | | - |
99 | | -Web components are asynchronous by nature. Importing the elements, registering them in the DOM, and finally initializing an element is done asynchronously. Therefore, you can't expect the element to work immediately after loading the page as a typical HTML element does. Consider the following example: |
100 | | - |
101 | | -```html |
102 | | -<link rel="import" href="bower_components/raml-js-parser/raml-js-parser.html"> |
103 | | -<raml-js-parser json></raml-js-parser> |
104 | | -<script> |
105 | | -var parser = document.querySelector('raml-js-parser'); |
106 | | -parser.loadApi(apiFileUrl); |
107 | | -</script> |
108 | | -``` |
109 | | - |
110 | | -Running this code as the page loads throws a `TypeError` with the message: `parser.loadApi is not a function`. |
111 | | - |
112 | | -At the time of execution of this script block, the browser knows nothing about the `raml-js-parser` element. At this time, the element is an instance of `HTMLUnknownElement`. You can read more about custom elements registration in our [starters guide to web components][2]. |
113 | | - |
114 | | -The browser has to import the source of the element first, and then the Polymer library has to register custom HTML element called `raml-js-parser`. |
115 | | - |
116 | | -To run the code properly you have to listen for the `WebComponentsReady` event. It's fired when the elements are ready to use. |
117 | | - |
118 | | -```html |
119 | | -<link rel="import" href="bower_components/raml-js-parser/raml-js-parser.html"> |
120 | | -<raml-js-parser json></raml-js-parser> |
121 | | -<script> |
122 | | -function init() { |
123 | | - var parser = document.querySelector('raml-js-parser'); |
124 | | - parser.loadApi(apiFileUrl); |
| 80 | +var MyAPiApp = { |
| 81 | + init: function() { |
| 82 | + var parser = document.querySelector('raml-js-parser'); |
| 83 | + parser.addEventListener('api-parse-ready', MyAPiApp._ramlReady); |
| 84 | + parser.loadApi('https://domain.com/api.raml'); |
| 85 | + window.addEventListener('raml-json-enhance-ready', MyAPiApp._jsonReady); |
| 86 | + }, |
| 87 | +
|
| 88 | + _ramlReady: function(e) { |
| 89 | + var enhacer = document.querySelector('raml-json-enhance'); |
| 90 | + enhacer.json = e.detail.json.specification; |
| 91 | + }, |
| 92 | +
|
| 93 | + _jsonReady: function(e) { |
| 94 | + // The e.detail.json contains the final JavaScript object |
| 95 | + var apiConsole = document.querySelector('api-console'); |
| 96 | + apiConsole.raml = e.detail.json; |
| 97 | + } |
125 | 98 | }; |
126 | | -window.addEventListener('WebComponentsReady', init); |
| 99 | +window.addEventListener('WebComponentsReady', MyAPiApp.init); |
127 | 100 | </script> |
128 | 101 | ``` |
129 | 102 |
|
130 | | -Because of differences between `v0` and `v1` spec of custom elements and fallback provided by the Polymer library read [our guide](api-console-element.md) of how to properly listen for the elements registration event. |
| 103 | +The `json` attribute set on `raml-js-parser` element tells the parser that the output should be a JavaScript object instead of the AST. |
131 | 104 |
|
132 | | -### Setting RAML data as an HTML attribute |
| 105 | +### Setting RAML data as a HTML attribute |
133 | 106 |
|
134 | 107 | The basic method for determining what API Console displays is to use the `raml` attribute. The attribute accepts the JavaScript object produced by the parser and the enhancer described above. |
135 | 108 |
|
|
0 commit comments