首页 > 解决方案 > Aurelia `@dynamicOptions` 与 `@bindable`s 混合

问题描述

我正在使用 aureliajs 进行开发。到目前为止,我正在处理一个已标记为具有动态选项的自定义属性。例如考虑以下代码:

  import {dynamicOptions, inject} from 'aurelia-framework';

  @dynamicOptions
  @inject(Element)
  export class SquareCustomAttribute {
    constructor(element){
      this.element = element;
    }

    propertyChanged(name, newValue, oldValue){
      switch(name){
        case 'fill':
          this.element.style.backgroundColor = newValue;
          break;
        case 'size':
          this.element.style.width = this.element.style.height = `${newValue}px`;
          break;
        default:
          this.element.style[name] = newValue;
          break;
      }
    }
  }

现在例如考虑我想给属性填充,defaultBindingModetwoWay,所以我会尝试@bindable在类中添加属性。所以代码会变成这样:

  import {dynamicOptions, inject} from 'aurelia-framework';

  @dynamicOptions
  @inject(Element)
  export class SquareCustomAttribute {
    @bindable fill;

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

    propertyChanged(name, newValue, oldValue){
      switch(name){
        case 'fill':
          this.element.style.backgroundColor = newValue;
          break;
        case 'size':
          this.element.style.width = this.element.style.height = `${newValue}px`;
          break;
        default:
          this.element.style[name] = newValue;
          break;
      }
    }
  }

绑定停止工作。

  1. 那么,第一个问题是如何为dynamicOptions设置defaultBindingMode?

  2. 还有一个小问题。正如aurelia.io所说,Binding to a dynamic options attribute works exactly the same as binding to an options attribute in the DOM.. 根据这句话,我希望绑定动态选项破折号并检索 in camel case onoptionsChanged方法。但是破折号分隔选项从视图中绑定,接收破折号分隔和骆驼,骆驼。根据aurelia.io的引用句子,这是正确的吗?

标签: dynamicoptionscustom-attributesaurelia

解决方案


感谢@bigopon,这个问题正在这个问题中得到跟进我认为在对 aurelia-framework 进行更改之前,任何额外的评论都将是有益的。


推荐阅读