Skip to content

Commit

Permalink
dcAsync now overrides getNamespace method of the wrapped component
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-github committed Nov 27, 2019
1 parent df2da22 commit 878b494
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
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: 0 additions & 4 deletions demo/test-async-component.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export default class TestAsyncComponent extends DcBaseComponent {
static getNamespace() {
return 'test-async-component'
}

onInit() {
this.element.innerHTML = 'Test async component was initialized';
}
Expand Down
11 changes: 8 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,17 @@ <h2>Lazy initialization</h2>
<div class="mb-5">
<h2>Async Loading</h2>
<p>
You can defer your components loading by changing the way of registering components. This technique uses Webpack dynamic import under the hood. So Webpack creates a separate chunk for the component on the build step.
You can defer your components loading by changing the way of registering components.
This technique compliments Webpack dynamic imports: webpack creates a separate chunk for the component on the build step.
</p>
<p>
The second argument in <i>dcAsync</i> should be the component's namespace.
This value will also replace components <i>getNamespace</i> static method so you don't have to declare component namespace twice.
</p>
<p>
The async loading is more powerful with lazy initialization.
In this case, the component's chunk will be loaded only after dcFactory tries to initialize it.
</p>
<p>The async loading is more powerfull with lazy initialization. In this case, the component's chunk will be loaded after dcFactory tries to initialize it.</p>
<div id="async-example" data-dc-lazy data-dc-test-async-component></div>
<button id="async-trigger-button"
class="btn btn-primary mt-2 mb-2">Init lazy component combined with async
Expand All @@ -221,7 +226,7 @@ <h2>Async Loading</h2>
})
</script>
<p>
In Network tab you can see that the whole code of the component is downloaded separately and can be easily moved to a separate async chunk
In Network tab you can see that the whole code of the component is downloaded separately
</p>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/dc-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default function dcAsync(importFunction, nameSpace) {
init() {
importFunction()
.then(({ default: componentClass }) => {
componentClass.getNamespace = function () {
return nameSpace;
};
this.actualInstance = new componentClass(this.element);
this.actualInstance.init();
})
Expand Down

0 comments on commit 878b494

Please sign in to comment.