Created
August 24, 2017 19:38
-
-
Save AshleyGrant/080923c09e13279529f22069ad6032d2 to your computer and use it in GitHub Desktop.
Passing data from processContent function to instance VM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <require from="./stupid-aurelia-tricks"></require> | |
| <stupid-aurelia-tricks> | |
| <child> | |
| One | |
| </child> | |
| <child> | |
| Two | |
| </child> | |
| </stupid-aurelia-tricks> | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { processContent, children, child, inlineView } from 'aurelia-framework'; | |
| @inlineView('<template><slot></slot></template>') | |
| @processContent(parseTemplate) | |
| export class StupidAureliaTricks { | |
| @child('process-content-data') processContentData: any; | |
| @children('child') children = []; | |
| attached() { | |
| console.log('this.processContentData.data', this.processContentData.data); | |
| console.log('this.children', this.children); | |
| } | |
| } | |
| function parseTemplate(compiler, resources, node, instruction) { | |
| const originalContent = node.innerHTML; | |
| const childCount = node.querySelectorAll('child').length; | |
| const data = { childCount }; | |
| // note the use of single quotes around the data.bind expression | |
| node.innerHTML = ` | |
| <process-content-data style="display: none" data.bind='${JSON.stringify(data)}'></process-content-data> | |
| ${originalContent} | |
| `; | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment