@bindable changeHandler在绑定完成更新之前触发

Vac*_*ano 3 javascript aurelia aurelia-binding

代码:

App.js

export class App {
  constructor() {
    this.widgets = [{ name: 'zero'}, {name: 'one'}, {name:'two'}];
    this.shipment = { widget: this.widgets[1] };
  }
}
Run Code Online (Sandbox Code Playgroud)

App.html

<template>
  <require from="./widget-picker"></require>
  <require from="./some-other-component"></require>

  <widget-picker widget.bind="shipment.widget" widgets.bind="widgets"></widget-picker>
  <some-other-component widget.bind="shipment.widget"/>
</template>
Run Code Online (Sandbox Code Playgroud)

窗口小部件,picker.js

import {bindable, bindingMode} from 'aurelia-framework';

export class WidgetPicker {
  @bindable({ defaultBindingMode: bindingMode.twoWay, changeHandler: 'widgetChanged'  }) 
  widget;

  @bindable widgets;

  widgetChanged(widget) {
      // Use an Event Aggregator to send a message to SomeOtherComponent
      // to say that they should check their widget binding for updates.
  }
}
Run Code Online (Sandbox Code Playgroud)

窗口小部件,picker.html

<select value.bind="widget">
  <option repeat.for="widget of widgets" model.bind="widget">${widget.name}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

问题:

@bindable的changeHandler将触发widgetChanged事件之前的绑定都更新到App.jsthis.shipment.widget.

因此,当Event Aggregator消息消失时,仍会在`this.shipment.widget'上设置先前的值.

题:

有没有办法让@bindablechangeHandler等到所有为@bindable更新的绑定都完成了?

或者我可以使用另一个回调吗?也许是一个改变过的手(过去时)?

我也尝试添加change.delegate="widgetChanged"select,希望该delegate选项将使其慢,但它仍是起火前更新全面铺开.

Jer*_*yow 5

您可以将需要执行的工作推送到微任务队列:

import {bindable, bindingMode, inject, TaskQueue} from 'aurelia-framework';

@inject(TaskQueue)
export class WidgetPicker {
  @bindable({ defaultBindingMode: bindingMode.twoWay, changeHandler: 'widgetChanged'  }) 
  widget;
  @bindable widgets;

  constructor(taskQueue) {
    this.taskQueue = taskQueue;
  }

  widgetChanged(widget) {
    this.taskQueue.queueMicroTask(
      () => {
        // Use an Event Aggregator to send a message to SomeOtherComponent
        // to say that they should check their widget binding for updates.
      });
  }
}
Run Code Online (Sandbox Code Playgroud)

这将确保它在事件循环的相同"转向"期间发生(而不是做类似的事情setTimeout(...)).