Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

计算属性的计算顺序问题 #29

Closed
otakustay opened this issue Jun 29, 2016 · 0 comments
Closed

计算属性的计算顺序问题 #29

otakustay opened this issue Jun 29, 2016 · 0 comments

Comments

@otakustay
Copy link
Member

otakustay commented Jun 29, 2016

当前计算属性的同步变化顺序是以计算属性的定义顺序来的,即:

this.defineComputedProperty('foo', ['x'], () => this.get('x') + 1);
this.defineComputedProperty('bar', ['x'], () => this.get('x') - 1);

x发生变化时,先计算foo,后计算bar

问题是当计算属性依赖另一个计算属性时,顺序不对就会导致问题,比如:

this.defineComputedProperty('bar', ['foo'], () => this.get('foo') - 2);
this.defineComputedProperty('foo', ['x'], () => this.get('x') + 1);

因为定义顺序顺序问题,barfoo先计算,那么这一次bar的值就是错的,以后也永远会是错的

#28 相结合,如果有重定义计算属性,如父类先定义了foo再定义了bar(此时顺序正确),子类又重定义了foo(导致顺序错误),这问题会是非常隐性并且难以排查的

现在的问题是:

  1. 计算属性是否要去建立一个依赖关系图来确定谁先计算谁后计算(代价不小)
  2. 如果建立这个依赖关系,之后的更新应该怎么做

事实上计算属性是一个很危险的事物,应该补一下文档说明下计算属性为什么危险,并不推荐广泛使用,以及从实践上指导应该怎么用

otakustay added a commit that referenced this issue Jun 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant