首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@bindable在绑定完成更新之前触发

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

Stack Overflow用户
提问于 2016-02-23 20:00:27
回答 1查看 486关注 0票数 4

“守则”:

App.js

代码语言:javascript
复制
export class App {
  constructor() {
    this.widgets = [{ name: 'zero'}, {name: 'one'}, {name:'two'}];
    this.shipment = { widget: this.widgets[1] };
  }
}

App.html

代码语言:javascript
复制
<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>

widget-picker.js

代码语言:javascript
复制
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.
  }
}

widget-picker.html

代码语言:javascript
复制
<select value.bind="widget">
  <option repeat.for="widget of widgets" model.bind="widget">${widget.name}</option>
</select>

问题是:

@bindable的changeHandler在绑定更新到App.js及其this.shipment.widget之前触发widgetChanged事件。

因此,当事件聚合器消息发出时,前面的值仍然设置在“the .shipment.widget”上。

问题:

是否有一种方法可以让@bindable**'s changeHandler等待,直到为@bindable更新的所有绑定都完成?**

或者我还能再用一次回调吗?也许是过去式( changedHandler )?

我确实尝试将change.delegate="widgetChanged"添加到select中,希望delegate选项会使其更慢,但在更新完全推出之前它仍然会触发。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-24 00:03:54

您可以将需要完成的工作推送到微任务队列中:

代码语言:javascript
复制
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.
      });
  }
}

这将确保它发生在事件循环的同一“转折”期间(而不是执行类似setTimeout(...)的操作)。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35587033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档