javascript - Aurelia attached triggers before repeat.for -


i'm trying setup logic in aurelia affect dom nodes looped repeat.for. if understand documentation correctly, attached() callback of view called after dom rendering , place put kind of logic in.

the problem attached() callback seems fired before repeat.for binding complete, leaving me partially rendered dom.

in order illustrate problem:

i have custom element containing:

<template>     <ul>       <li repeat.for="thing of things"></li>     </ul> </template> 

once attached called(), expect having rendered dom containing li elements, instead. simple dump of dom shows empty

how can implement callback gets access li nodes?

attached called when component's dom element "attached" dom. there may child components such repeated templates further down in queue rendered, easiest thing put logic @ bottom of queue:

import {inject, taskqueue} 'aurelia-framework';  @inject(taskqueue) export class mycomponent {   constructor(taskqueue) {     this.taskqueue = taskqueue;   }    dosomethingafterrepeatisrendered() {     // logic...   }    attached() {     this.taskqueue.queuemicrotask({       call: () => this.dosomethingafterrepeatisrendered();     });   } } 

there better approaches this, need know more kind of work need <li> elements provide better answer. it's uncommon use taskqueue directly, things can refactored custom elements or attributes participate in composition lifecycle more naturally. example, if need jquery stuff <li> elements, "aurelia way" separate logic view-model using custom attribute:

do-something.js

import {inject, customattribute} 'aurelia-framework'; import $ 'jquery';  @customattribute('do-something') @inject(element) export class dosomethingcustomattribute {   constructor(element) {     // "element" dom element rendered     // template attribute appears on, in example <li> element     this.element = element;   }        // don't need task queue because attached called when    // <li> element attached.   attached() {     // this.value whatever custom attribute bound to, in case     // "thing" "things" array.     $(this.element).dosomething(this.value);   } } 

here's usage:

app.html

<template>   <require from="./do-something"></require>    <ul>     <li repeat.for="thing of things" do-something.bind="thing"></li>   </ul> </template> 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -