简体   繁体   English

Aurelia自定义元素实例创建

[英]Aurelia custom element instance creation

How to initiate a creation of new instance of custom element when the bound object is updated? 当绑定对象更新时,如何启动创建自定义元素的新实例? Let's say I have a visitDate in the view that can be changed by a user. 假设我在视图中有一个visitDate,用户可以更改它。 When it's changed, I want to update the content of the page. 更改后,我想更新页面的内容。 View-model: 视图模型:

...
visitDateChanged() {
   this.shifts = [];
   this.newShift = {};
   this.newShift = {pstart: this.visitDate,  pstop: this.visitDate});
   this.service.getData(this.visitDate)
      .then(result => this.shifts = result);
    }
}

view: 视图:

<div repeat.for="shift of shifts | filterDate: visitDate">
    <shift-form shift.bind="shift" shifts.bind="shifts"></shift-form>
</div>
<div>
    <shift-form shift.bind="newShift" shifts.bind="shifts"></shift-form>
</div>

Note: shift-form is a custom element. 注意:shift-form是一个自定义元素。 Now if I change visitDate in the view, it calls visitDateChanged() and updates newShift and shifts variables. 现在,如果我在视图中更改了visitDate,它将调用visitDateChanged()并更新newShift并移动变量。 It detaches all shift-form instances called inside div repeat.for and attaches new instances according to objects inside shifts array. 它分离所有在div repeat.for内部调用的shift形式的实例,并根据shifts数组内的对象附加新实例。 BUT it doesn't detach and reattach shift-form (doesn't create a new instance of shift-form) inside the second div. 但是它不会在第二个div内分离并重新附加shift-form(不会创建shift-form的新实例)。 And I need a new instance of shift-form inside div when newShift is recreated. 当重新创建newShift时,我需要在div中有一个shift形式的新实例。 I managed to do it by changing newShift from an object to an array of one. 我设法通过将newShift从一个对象更改为一个数组来做到这一点。 Then 然后

<div repeat.for="shift of newShift">
    <shift-form shift.bind="shift" shifts.bind="shifts"></shift-form>
</div>

does the trick but I don't think it's an elegant solution 可以解决问题,但我认为这不是一个优雅的解决方案

As Ashley Grant suggested I have a reset function, but I am not listening for a change. 正如阿什利·格兰特(Ashley Grant)所建议的那样,我具有重置功能,但是我没有听更改。 I have a ref for the element and I call the reset function outside when needed. 我有一个ref的元素,并在需要时在外部调用reset函数。

<some-element controller.ref='someVariableNameHere'></some-element>

and in the controller that uses this ref: 并在使用此引用的控制器中:

this.someVariableNameHere.viewModel.reset();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM