【问题标题】:Aurelia - when are child compositions done?Aurelia - 儿童作文什么时候完成?
【发布时间】:2017-03-31 07:21:41
【问题描述】:

使用 Aurelia JS 框架,我需要能够检测到重复元素的结果何时已通过父 VM 中的代码完全加载到 DOM 中。我知道我可以将侦听器注入@bindable 并在附加()上触发侦听器,但这对我来说似乎很不礼貌。

有什么想法吗?

【问题讨论】:

标签: javascript aurelia aurelia-templating


【解决方案1】:

问题是您在 attached 方法中推送项目并且您正在使用动态组合,这通常不是问题,但您的情况有点不同。排队微任务没有帮助,因为它在您的视图(child.html)加载之前运行。

有几种方法可以解决这个问题:

1°解

将您的物品推入bind()

bind() {
  this.items.push({
     view: 'child.html',
     viewModel: new Child('A')
  });  

  this.items.push({
     view: 'child.html',
     viewModel: new Child('B')
   });
}

attached() {

  let container = document.getElementById('container');
  console.log('Height after adding items: ' + container.clientHeight);

  this._taskQueue.queueMicroTask(() => {
    console.log('Height after queued microtask: ' + container.clientHeight);    
  });

  setTimeout(() => {
    console.log('Height after waiting a second: ' + container.clientHeight);    
  }, 1000);

}

修复 HTML:

<template>
  <div id="container">
    <div repeat.for="item of items">
      <compose view-model.bind="item.viewModel" view.bind="item.view"></compose>
    </div>
  </div>
  <div style='margin-top: 20px'>
    <div repeat.for="log of logs">
      ${log}
    </div>
  </div>
</template>

运行示例https://gist.run/?id=7f420ed45142908ca5713294836b2d5e

2º解决方案

不要使用&lt;compose&gt;。当需要动态组合时,可以使用 Compose。您确定需要使用&lt;compose&gt;?。

<template>
  <require from="./child"></require>

  <div id="container">
    <div repeat.for="item of items">
      <child>${item.data}</child>
    </div>
  </div>
</template>

【讨论】:

  • Bind 似乎在等待真实高度可用,但之后我需要能够在任何时候检查高度。我在 gist 中添加了一个 click(),作为示例添加了另一个元素。
  • 至于不使用 compose,我需要使用 compose,因为我的应用程序有不同的视图需要根据模型属性进入列表(就像在要点中一样,虽然要点只有一个查看)。
  • 我已经用一个可行的解决方案更新了我的答案和我的要点gist.run/?id=7f420ed45142908ca5713294836b2d5e,但可能有更好的方法来解决这个问题,看看它是否有帮助。与此同时,我会尽力寻求帮助
  • 谢谢,法比奥。我考虑了回调方式,但我不想在所有可能的子组件中实现它,似乎应该有一种方法可以使用框架从父级处理这个问题。
  • 我正在研究它,我也请求官方团队的帮助。让我们看看我明天或后天是否有解决方案
猜你喜欢
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 2015-05-06
相关资源
最近更新 更多