简体   繁体   English

Aurelia使用ES5计算属性

[英]Aurelia computed properties with ES5

I'm working through an Aurelia tutorial but intentionally only using ES5 and AMD/RequireJS since initially I'm trying to reduce the technical overload which I may need to introduce in my current production app (which is currently using Angular but I'm thinking of swapping to Aurelia instead of Angular 2) but I'm stuck with getting computed properties working. 我正在通过Aurelia教程,但故意只使用ES5和AMD / RequireJS,因为最初我正在尝试减少技术过载,我可能需要在我当前的生产应用程序中引入(目前正在使用Angular但我正在考虑交换到Aurelia而不是Angular 2)但我仍然坚持让计算属性工作。

I've noticed that an update made in April removed the computed function from the Object prototype which allowed the following syntax but I'm not sure what I should do instead of the following syntax: 我注意到4月份更新从Object原型中删除了计算函数,它允许使用以下语法,但我不确定应该做什么而不是以下语法:

Welcome.computed({
    fullName : function() { return this.firstName + " " + this.lastName; }
});

I can achieve the same effect with the following but it seems quite verbose given what's being achieved! 我可以通过以下方式获得相同的效果,但考虑到实现的目标,它似乎相当冗长! Is there a correct Aurelia way? 是否有正确的Aurelia方式?

Object.defineProperty(
  Welcome.prototype,
  "fullName",
  {
    get: function() { return this.firstName + " " + this.lastName },
    enumerable: true
  });

What you're doing is correct- you could always re-add the computed factory method since writing all this extra ES5 code could get tedious. 你正在做的是正确的 - 你总是可以重新添加计算出的工厂方法,因为编写所有这些额外的ES5代码可能会变得乏味。

I'd also recommend specifying the computed property's dependencies to optimize data-binding efficiency. 我还建议指定计算属性的依赖项以优化数据绑定效率。 This following code snippets are equivalent: 以下代码段是等效的:

ES5: ES5:

function fullName() { return this.firstName + ' ' + this.lastName }
fullName.dependencies = ['firstName', 'lastName'];
Object.defineProperty(
  Welcome.prototype,
  'fullName',
  {
    get: fullName,
    enumerable: true
  });

ES6/ES7: ES6 / ES7:

@computedFrom('firstName', 'lastName')
get fullName() {
  return `${this.firstName} ${this.lastName}`;
}

http://aurelia.io/docs.html#adaptive-binding http://aurelia.io/docs.html#adaptive-binding

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

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