当前位置:网站首页>Structure of ViewModel
Structure of ViewModel
2022-07-05 02:24:00 【hxa12345】
1、ViewModel:
abstract class , There are mainly clear Method , It is final level , Do not modify the ,clear Method contains onClear hook , Developers can rewrite onClear Method to define the emptying of data
2、ViewModelStore:
Internal maintenance one HashMap To manage ViewModel
3、ViewModelStoreOwner:
Interface ,ViewModelStore Scope of action , The implementation class for ComponentActivity and Fragment, Besides, there are FragmentActivity.HostCallbacks
4、ViewModelProvider:
Used to create ViewModel, The construction methods are Two parameters :
The first parameter is passed in ViewModelStoreOwner , To determine the ViewModelStore Scope of action ;
The second parameter is ViewModelProvider.Factory, For initialization ViewModel object , The default is getDefaultViewModelProviderFactory() Method acquired factory.
5、 Several ways to implement two-way binding
At present, several mainstream mvc(vm) The framework implements one-way data binding , And the two-way data binding that I understand is nothing more than to give input elements on the basis of one-way binding (input、textare etc. ) Added change(input) event , To dynamically modify model and view, Not so deep . So there's no need to worry too much about one-way or two-way binding of implementation .
There are several ways to implement data binding :
Publisher - Subscriber pattern (backbone.js)Dirty value check (angular.js)
The data was hijacked (vue.js)
Publisher - Subscriber pattern : Usually by sub, pub Data and view binding monitoring , The usual way to update data is vm.set('property', value)
, Not familiar. Ask Du Niang
This way, after all, is too low 了 , We prefer to pass vm.property = value
Update data this way , Automatically update the view at the same time , So there are two ways
Dirty value check : angular.js It is to compare whether there is any change in data through dirty value detection , To decide whether to update the view , The easiest way is through setInterval()
Regular polling detects data changes , Of course Google Not so low,angular Enter dirty value detection only when the specified event is triggered , As follows :
- DOM event , For example, users input text , Click button, etc .( ng-click )
- XHR Responding to events ( $http )
- browser Location Change event ( $location )
- Timer event ( $timeout , $interval )
- perform $digest() or $apply()
The data was hijacked : vue.js Data hijacking combined with publishers - How subscriber mode works , adopt Object.defineProperty()
To hijack the setter
,getter
, Publish messages to subscribers when data changes , Trigger corresponding listening callback .
Train of thought
I have learned that vue Data binding is done through data hijacking , The most important way is through Object.defineProperty()
To achieve the hijacking of properties , To monitor data changes , No doubt this method is the most important in this article 、 One of the most basic elements , If you're not familiar with defineProperty, stab here Sort it out , To achieve mvvm Two-way binding of , We must realize the following points :
- Implement a data listener Observer, The ability to listen for all attributes of a data object , Updated values are available and subscribers are notified of any changes
- Implement an instruction parser Compile, Scan and parse the instructions of each element node , Replace the data according to the instruction template , And bind the corresponding update function
- Achieve one Watcher, As the connection Observer and Compile The bridge , The ability to subscribe and receive notification of each property change , Executes the corresponding callback function for the instruction binding , To update the view
- mvvm Entry function , Integrate the above three
边栏推荐
- LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
- Go RPC call
- Openresty ngx Lua Execution stage
- openresty ngx_lua执行阶段
- openresty ngx_lua变量操作
- Advanced learning of MySQL -- Application -- Introduction
- 使用druid連接MySQL數據庫報類型錯誤
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
- [機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
- 低度酒赛道进入洗牌期,新品牌如何破局三大难题?
猜你喜欢
Codeforces Global Round 19 ABC
Pytest (5) - assertion
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Bert fine tuning skills experiment
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
openresty ngx_ Lua execution phase
Unpool(nn.MaxUnpool2d)
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
Naacl 2021 | contrastive learning sweeping text clustering task
如何做一个炫酷的墨水屏电子钟?
随机推荐
Richview trvunits image display units
openresty ngx_lua執行階段
Pytest (5) - assertion
Tucson will lose more than $400million in the next year
Video display and hiding of imitation tudou.com
Start the remedial work. Print the contents of the array using the pointer
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
Prometheus monitors the correct posture of redis cluster
如何搭建一支搞垮公司的技术团队?
Binary tree traversal - middle order traversal (golang)
Pytorch fine tuning (Fortune): hollowed out design or cheating
Last week's hot review (2.7-2.13)
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
Vulnstack3
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
Li Kou Jianzhi offer -- binary tree chapter
Openresty ngx Lua Execution stage
Visual explanation of Newton iteration method
Application and Optimization Practice of redis in vivo push platform
Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming