我有这个属性装饰器,它通过转换器运行属性值,所以我可以在视图模板中使用some-bindable-value="true"
:
function valueConverter(converter: Function) {
return (target: any, key: string) => {
let definition = Object.getOwnPropertyDescriptor(target, key);
if (definition) {
Object.defineProperty(target, key, {
get: definition.get,
set: newValue => {
definition.set(converter(newValue));
},
enumerable: true,
configurable: true
});
} else {
Object.defineProperty(target, key, {
get: function () {
return this['__' + key];
},
set: function (newValue) {
this['__' + key] = converter(newValue);
},
enumerable: true,
configurable: true
});
}
}
}
class App {
@valueConverter(value => value == 'true') public someBooleanValue;
constructor() {
this.someBooleanValue = 'false';
console.log(this.someBooleanValue) // false
this.someBooleanValue = 'true';
console.log(this.someBooleanValue) // true
}
}
只要我不使用@bindable
装饰器(这使它完全没有意义),这样就可以正常工作。我是装修人员的新手,我不知道如何使用Aurelia的财产观察机制。
答案 0 :(得分:1)
我想如果我想要完成这个,我首先复制bindable
装饰器的代码,然后将自己的代码添加到它。它位于:https://github.com/aurelia/templating/blob/master/src/decorators.js
查看observable
装饰器如何执行操作可能会有所帮助:https://github.com/aurelia/binding/blob/master/src/decorator-observable.js
看起来你可能不得不让自己的手变得很脏以实现你想要的东西。那就是说,如果你能够实现它,我们就会喜欢它回到框架中在某些时候公关!