Ember计算属性是否与/包含异步代码一起使用? [英] Were Ember Computed Properties meant to be used with / contain asynchronous code?

查看:160
本文介绍了Ember计算属性是否与/包含异步代码一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名经验丰富的Ember.js开发人员。在指南中,我们可以找到具有全名的Computed Property(同步,简单,依赖于姓氏和姓氏)的示例。在野外,我们可以以异步的方式找到计算属性的许多用法(例如,在承诺解决之后设置本身,而第一次运行并获得返回 undefined )。



我看到的异步计算属性越多,我想知道的是计算属性意味着与异步代码一起使用吗?难道不是要麻烦吗?



一个常见的问题是其他计算属性(CP2)依赖于异步CP1。 CP2得到CP1,但它得到 undefined (因为CP1将在稍后的时间内设置它的值,因为它是异步的)。 CP2以CP2的错误值( undefined )完成计算。 CP1设置自身,但CP2不再重新计算(即使CP1已更改),因为CP2未在模板中引用(这意味着它是绑定的,并且其值始终是必需的,CP1始终重新计算) - 而是通过一些JavaScript调用引用。



这个真实世界的例子可以是根据订单中的项目计算总订单(来自电子商务店)的价格。计算属性依赖于与项目的异步关系,可能包含其他异步关系,例如税收类型。

解决方案

我假设你的意思是混合计算属性和承诺。如果你不这样做我多次绊倒这个问题。我觉得有问题,尤其是深层嵌套的关联。



我也在一个电子商务网站上工作。最初,我发现很多计算在网站呈现时没有得到解决。原因是因为我通过承诺到计算属性(用于计算)。后来,我意识到我应该在将结果传递给计算逻辑之前解决所有的关系。我做了这一步服务。一个例子来证明我的意思:



我们来说,订单有很多项目,你想计算总价格,价格是项目中的一个字段。



而不是:

 总计:Ember.computed(order.items。价格,function(){
return this.get(order.items)。reduce((sum,obj)=> {
return sum + obj.get(price);
});
});

我这样做:

 总计:Ember.computed(items。@。price,function(){
return this.get(items)。reduce((sum,obj)=&
return sum + obj.get(price);
});
});

其中项目作为承诺传入结果在某个地方。



我发现这个帖子解释为什么你不应该相当好。



我试着一回问问类似的问题。想要听到更多的想法。


I'm an experienced Ember.js developer. In guides, we can find an example of Computed Property with full name (synchronous, easy, relies on first name and last name). In the wild, we can find however lots of usages of Computed Properties in an asynchronous manner (for example setting itself after promises resolve - while the first run and get returns undefined).

The more I see this asynchronous Computed Properties the more I wonder - were Computed Properties meant to be used with an asynchronous code? Isn't it asking for trouble?

A common issue is that other Computed Property (CP2) relies on async CP1. CP2 gets CP1 but it gets undefined (as CP1 will set its value in later time as it is async). CP2 finishes its calculation with the wrong value of CP2 (undefined). CP1 sets itself, but CP2 does not recalculate anymore (even CP1 changed) because CP2 isn't referenced in the template (which would mean it is bound and its value is needed all the time, always recalculates when CP1 changes) - but instead was referenced by some JavaScript call.

The real world example of this could be the calculation of total order (from e-commerce shop) price based on items in the order. Computed property relies on asynchronous relationships to items, which could contain other asynchronous relationships, like tax type.

解决方案

I assume you mean mixing computed properties and promises. If you do then don't. I have stumbled upon this issue multiple times. I find it problematic especially with deep nested associations.

I was working on an eCommerce site as well. Initially, I discovered a lot of calculations didn't get resolved when the site rendered. The reason was because I passed promises into computed property(which is used for calculation). Later on, I realized I should resolve all relationships before passing results into calculation logics instead. I did this step in a service. One example to demonstrate what I mean:

Let's say, order has many items and you want to calculate total price, price is a field in item.

Instead of:

total: Ember.computed("order.items.@.price", function() {
  return this.get("order.items").reduce((sum, obj) => {
    return sum + obj.get("price");
  });
});

I do this:

total: Ember.computed("items.@.price", function() {
   return this.get("items").reduce((sum, obj) => {
     return sum + obj.get("price");
   });
});

where items is passed in as a promise result somewhere above.

I found this post explains why-you-should-not quite well.

I tempted to ask similar question a while back. Keen to hear more ideas about this.

这篇关于Ember计算属性是否与/包含异步代码一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆