Skip to content

Instantly share code, notes, and snippets.

@AshleyGrant
Created August 24, 2017 19:38
Show Gist options
  • Save AshleyGrant/080923c09e13279529f22069ad6032d2 to your computer and use it in GitHub Desktop.
Save AshleyGrant/080923c09e13279529f22069ad6032d2 to your computer and use it in GitHub Desktop.
Passing data from processContent function to instance VM
<template>
<require from="./stupid-aurelia-tricks"></require>
<stupid-aurelia-tricks>
<child>
One
</child>
<child>
Two
</child>
</stupid-aurelia-tricks>
</template>
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