MVVM Code Refactoring specification

Source: Internet
Author: User

To facilitate the maintenance of code after the MVVM refactoring, the following specifications have been developed:
First, Responsibility:
The responsibilities of VC (i.e. Viewcontroller):
1) Process the page layout and the life cycle of the page (viewdidload,viewwillappear, etc.)
2) respond to all interactions on the page, including button clicks, gestures, etc.
3) Listen to changes in the data in the VM, subscribe to the signal, according to the contents of the signal to make corresponding changes.
4) manage the creation and destruction of page-related VMS
Responsibilities for VMS (i.e., ViewModel):
1) Handle network requests, read and write local caches, and the corresponding view logic for these operations (including cue boxes, page jumps, etc.) signal input
2) Save the view to show the required data, including the data required for VC initialization
3) Handle other logic that does not depend on the page (such as saving a logged-in user name)
Ii. some rules for creating VMS:
1) One type of business creates only one VM
2) one page facing multiple businesses, multiple VMS can be created
3) The granularity of the division should be as thin as possible, so that similar business logic can use the same VM. Can be divided according to the network interface. (e.g. QMLOGINVM)
Third, VC and VM Communication mode:
Hook-up via REACTIVECOCOA signal
1) Network request unified with racsignal processing, in the signal block to execute the request, received a response to send next signal, pass status to the VC. Status is not saved in the VM.
2) VC subscription signal, according to the status value of next signal, to do the corresponding operation.
3) The display or close of the prompt box, the Enable state of the button, etc. can be processed by using the racsubject signal alone, the input signal from the VM, the VC subscription.
4) for TableView required data, unified in the VM to save data, VC through Racobserve or KVO to listen.
Iv. Block and delegate
The network framework supports both block and delegate two ways, the development of the module to differentiate, if the previous code is used delegate, continue to use delegate, and vice versa; if it is a newly developed module, select it according to your preference.

MVVM Code Refactoring specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.