简体   繁体   English

Aurelia自定义属性中的双向绑定

[英]Two-Way binding in an Aurelia Custom Attribute

UPDATE: It looks like this is a known bug: https://github.com/aurelia/templating/issues/253 更新:看起来这是一个已知的错误: https//github.com/aurelia/template/issues/253
I am leaving it here for reference / searchability purposes. 我将其留在此处以供参考/可搜索性目的。

The Code: 代码:

input-mask.ts (Full code can be seen here ) input-mask.ts (完整的代码可以在这里看到)

@customAttribute('input-mask')
@inject(Element)
export class InputMaskCustomAttribute {

    @bindable({ defaultBindingMode: bindingMode.twoWay,
                changeHandler: 'onUnmaskedValueChanged'  })
    unmaskedValue: any;

    onUnmaskedValueChanged(newValue, oldValue) {
        console.log('unmaskedValue updated from inside the custom attribute');
    }

    @bindable
    mask: string;

    attached() {

          this.eventTarget.on('focusout', (e: any) => {
             this.unmaskedValue = (<any>$(this.element)).cleanVal()
             this.fireEvent(e.target, 'input');
          });
    }

  // Code for constructor, fireEvent and to setup the mask...
}

carrier.html carrier.html

<input input-mask="mask.bind: carrier.airbillMask; unmasked-value.bind: airbill" 
       value.bind="formattedAirbill"/>

UPDATE: To work around this bug, change to unmasked-value.two-way and the binding will work. 更新:要变通解决此错误,更改为unmasked-value.two-way ,绑定将起作用。

carrier.ts 载体

@bindable({ defaultBindingMode: bindingMode.twoWay})
carrier: EntityInterfaces.ICarrier;

@bindable({ defaultBindingMode: bindingMode.twoWay })
formattedAirbill: string;

@bindable({ defaultBindingMode: bindingMode.twoWay, changeHandler: 'onAirbillChanged' })
airbill: string;

onAirbillChanged() {
    console.log('Airbill was set!');
}

The Problem: 问题:

The data seems to flow into the @bindable variable just fine. 数据似乎很好地流入了@bindable变量。 As the mask changes, the value in the custom attribute is changed. 随着蒙版的更改,自定义属性中的值也会更改。

But it does not seem to flow out if changes are made inside the custom-attribute. 但是,如果在自定义属性内进行更改,它似乎不会消失。

Example Scenario: After I edit the value in the input box, and exit the input, the focusout event fires and the console statement that indicates that the unmasked Value was updated from inside the custom attribute prints: 示例场景:在输入框中编辑值并退出输入后,将触发focusout事件,并显示控制台语句,该语句指示从自定义属性内部更新了未屏蔽的Value:

unmaskedValue updated from inside the custom attribute unmaskedValue从自定义属性内部更新

But (when the input looses focus) the console statement saying that the airbill on the carrier.ts file was updated does not fire when I exit the input box: 但是(当输入失去焦点时)控制台语句说,当我退出输入框时, airbill文件上的airbill已更新不会触发:

This does not fire: 这不会触发:
console.log('Airbill was set!'); console.log('Airbill已设置!');

This seems to show to me that the binding is not really two-way. 这似乎向我表明,绑定实际上不是双向的。

The Question: 问题:

How can I make this binding two-way? 我该如何进行双向绑定? So that when I update unmaskedValue inside the custom-attribute it will update the bound value in the view model? 这样,当我在自定义属性内更新unmaskedValue时,它将更新视图模型中的绑定值吗?

Note: As a workaround, I was able change unmasked-value.bind to be a method call ( on-unmasked-value-changed.call="onUnmaskedValueChanged($event) ) and update the value in that method. So I don't NEED this to work. But I would like to know if it is possible for future use. 注意:作为一种解决方法,我可以将unmasked-value.bind更改为方法调用( on-unmasked-value-changed.call="onUnmaskedValueChanged($event) )并更新该方法中的值。所以我不t需要此功能,但我想知道将来是否可以使用。

此已知错误已于2016年3月15日修复并关闭https://github.com/aurelia/templating/issues/253#issuecomment-189394955

Try to initialize the variable unmaskedValue with default value. 尝试使用默认值初始化变量unmaskedValue。 Try null, undefined, '' and so on. 尝试null,undefined,''等。 I have done this before but I don`t remember in which version (certainly it was beta) 我以前做过,但是我不记得是哪个版本(肯定是beta版)

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

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