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

用push的方法往一个已创建的Element里插入childern,不能自动更新Element的count #4

Closed
ibufu opened this issue Dec 28, 2015 · 7 comments

Comments

@ibufu
Copy link

ibufu commented Dec 28, 2015

每次增加删除childern,都要手动重新计算Element的count,很麻烦

@livoras
Copy link
Owner

livoras commented Dec 28, 2015

@ibufu 一般情况下是不会直接操作Element的children的,Element应该在每次状态变更的时候重新构造。不知道你的问题具体是什么,能否提供一下具体代码呢?

@ibufu
Copy link
Author

ibufu commented Dec 29, 2015

@livoras 建好了一个Element后,需要做增删改,所以要往element的children里push,或者根据我设置的id遍历children修改数据。(因为只是很局部的操作)操作起来很麻烦,而且push的时候,还要更新count,不然diff的时候index就会出错。

@ibufu
Copy link
Author

ibufu commented Dec 29, 2015

@livoras 如果能开一个方便修改element的接口就好了

@livoras
Copy link
Owner

livoras commented Dec 29, 2015

@ibufu 即使是非常小的局部修改,也应该重新构造整棵虚拟DOM。一个虚拟DOM树一旦构造就不应该手动修改,它只是保存目前DOM树的状态。状态变更就应该构造新的树,再和原来的树进行diff。否则diff出来的patches就不正确了。

@ibufu
Copy link
Author

ibufu commented Dec 29, 2015

@livoras 我明白你的意思,但是这样构建DOM树确实有点点麻烦。。如果能支持判断循环的方法就好了

@livoras
Copy link
Owner

livoras commented Dec 29, 2015

@ibufu 把构造虚拟DOM的操作封装成函数,每次状态改变就去调用这个函数重新构造就好了:https://github.com/livoras/simple-virtual-dom/blob/master/example/increasing-items.html#L20

@livoras
Copy link
Owner

livoras commented Dec 30, 2015

有后续类似问题可以重开此issue :-)

@livoras livoras closed this as completed Dec 30, 2015
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

2 participants