当前位置:网站首页>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
边栏推荐
- Pytorch common code snippet collection
- Abacus mental arithmetic test
- Process scheduling and termination
- Introduce reflow & repaint, and how to optimize it?
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- Word processing software
- Pytest (5) - assertion
- openresty ngx_lua变量操作
- runc hang 导致 Kubernetes 节点 NotReady
- He was laid off.. 39 year old Ali P9, saved 150million
猜你喜欢
Exploration of short text analysis in the field of medical and health (I)
力扣剑指offer——二叉树篇
Practice of tdengine in TCL air conditioning energy management platform
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
Pytest (5) - assertion
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
Restful fast request 2022.2.1 release, support curl import
CAM Pytorch
随机推荐
Yolov5 model training and detection
【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)
Stored procedure and stored function in Oracle
Educational Codeforces Round 122 (Rated for Div. 2) ABC
The phenomenology of crypto world: Pioneer entropy
One click generation and conversion of markdown directory to word format
Video display and hiding of imitation tudou.com
[download white paper] does your customer relationship management (CRM) really "manage" customers?
February database ranking: how long can Oracle remain the first?
Pytest (4) - test case execution sequence
CAM Pytorch
Design and practice of kubernetes cluster and application monitoring scheme
力扣剑指offer——二叉树篇
Grub 2.12 will be released this year to continue to improve boot security
Which common ports should the server open
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
100 basic multiple choice questions of C language (with answers) 04