【问题标题】:Access a custom attribute's binding from another custom attribute从另一个自定义属性访问自定义属性的绑定
【发布时间】:2017-03-17 21:36:06
【问题描述】:

假设我有一个正在使用的 Aurelia 自定义属性,是否可以在绑定到另一个自定义属性的值发生更改时触发事件?

更具体的说,我正在为 Chosen 构建一个 Aurelia 自定义属性。我得到了所有选择的工作。

但是<select> 元素上有一个focus.bind="isFocused"。我需要知道(在我选择的自定义属性中)该绑定值何时发生变化。

有什么方法可以在我选择的自定义属性中访问该焦点绑定(从焦点自定义属性)?也许使用绑定引擎甚至内部方法?

(注意:我考虑过制作一个自定义元素来执行此操作,但 Aurelia 不能很好地用于包装元素。我必须为样式、类和原始选择组件上的任何其他内容编写连接。)

【问题讨论】:

标签: javascript aurelia


【解决方案1】:

我不确定这是否是正确的做法,但这是迄今为止我发现的最简单的方法。

my-attr.js

import { inject } from 'aurelia-framework';
import { Focus } from 'aurelia-templating-resources';

@inject(Focus)
export class MyAttrCustomAttribute {

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

  attached() {
    console.log(this.focusAttr.value); //bound value

    //...
    //extending the change event. BE CAREFUL!
    //...

    const originalValueChanged = this.focusAttr.valueChanged;
    this.focusAttr.valueChanged = function(newValue) {
      console.log('Focus bound value has changed!'); //<-- call an event before "focus"
      originalValueChanged.call(this, newValue); //<-- "focus" is enqueued
      // <--- enqueue an event event here to be called after "focus"
    }

  }
}

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

【讨论】:

    【解决方案2】:

    尝试使用绑定行为来拦截绑定的updateSource(更新视图模型)和updateTarget(更新视图)方法。 自定义绑定行为部分下的文档中有一个运行示例。

    http://aurelia.io/hub.html#/doc/article/aurelia/binding/latest/binding-binding-behaviors/8

    【讨论】:

    • 如果第二个绑定是可选的呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2011-07-19
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多