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.js和this.shipment.widget.
因此,当Event Aggregator消息消失时,仍会在`this.shipment.widget'上设置先前的值.
有没有办法让@bindablechangeHandler等到所有为@bindable更新的绑定都完成了?
或者我可以使用另一个回调吗?也许是一个改变过的手(过去时)?
我也尝试添加change.delegate="widgetChanged"到select,希望该delegate选项将使其慢,但它仍是起火前更新全面铺开.
您可以将需要执行的工作推送到微任务队列:
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(...)).
| 归档时间: |
|
| 查看次数: |
337 次 |
| 最近记录: |