-
Notifications
You must be signed in to change notification settings - Fork 2
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
从一个简单的todo-app开始:MV*实战 #71
Comments
MVVM框架从实现一个简单的todo开始。效果如下 我们先现在用最原始的方式来实现它。代码如下:
这样的代码的复杂度在于dom结构和状态耦合在一起。耦合意味着某个数据的结果依赖于其他数据的计算。也就是说dom的修改可能会引起状态的变化。而这个变化不一定是我们期待的变化,即会引出bug。 比如当我们在todoTemplate的li元素上的class属性上添加todo,代码如下
原来我们是通过 MVC现在我们使用MVC模式来重构代码:
我们可以看出MVC模式下的代码。状态(状态即model)和dom进行了解耦。 虚线表示监听关系。 图中两条虚线分别表示:
1,2,3则表示当界面上发生用户交互后,它们三则之间的调用顺序。如下:
通过MVC模式编写代码,我们需要手动操作DOM,随着view的膨胀,我们需要操作的DOM就越来越多,代码的复杂度也越高。 MVVM现在我们使用MVVM框架
所以MVVM的目的是当model层发生变化时,view层能够自动更新。而不用我们手动操作DOM,从而减少代码的复杂度。 相比于原生 JavaScript,现在流行的 JS 框架 React 和 Vue 都解决了什么问题? |
The text was updated successfully, but these errors were encountered: