Skip to content

Commit

Permalink
#8 // explicitly pass root to dcFactory.init method
Browse files Browse the repository at this point in the history
  • Loading branch information
bububo committed Feb 27, 2020
1 parent dab82ff commit eac6d76
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dcFactory.register(CollapsedComponent);


// later after registering all your components, when your page is ready
dcFactory.init();
dcFactory.init(document.body);

```

Expand All @@ -67,13 +67,13 @@ Type: `string`

CSS selector which will override searching by getNamespace() and be used for searching elements of given componentClass.

### dcFactory.init(root = document.body, withLazy = true)
### dcFactory.init(root, withLazy = true)

Starts the factory on a given root: finds and creates all registered components within the root

#### root

*Optional*<br>
*Required*<br>
Type: `HTMLElement`

#### withLazy
Expand Down Expand Up @@ -139,4 +139,4 @@ Type: `HTMLElement`


## License
[MIT](https://choosealicense.com/licenses/mit/)
[MIT](https://choosealicense.com/licenses/mit/)
2 changes: 1 addition & 1 deletion demo/dist/dc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/dist/dc.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ <h2>Simple example</h2>

<p>
Call dcFactory.register with your components, and then starts the factory by calling
dcFactory.init()
dcFactory.init(document.body)
</p>

<script data-live-highlight>
dcFactory.register(CollapsedComponent);
dcFactory.init();
dcFactory.init(document.body);
</script>
</div>

Expand Down
7 changes: 6 additions & 1 deletion src/dc-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ class DcFactory {
* @param {HTMLElement} root
* @param {boolean} withLazy - Whether or not initialize component which marked as lazy
*/
init(root = document.body, withLazy = true) {
init(root, withLazy = true) {
if (!(root instanceof HTMLElement)) {
console.error(`Can not initialize components in ${root}. The first argument must be an HTMLElement`);
return;
}

this._registredComponents.forEach(({ componentClass, selector }) => {
this._initComponent(root, componentClass, selector, withLazy);
});
Expand Down

0 comments on commit eac6d76

Please sign in to comment.