Model – View – Controller: Are input fields views or controllers?

I am having a hard time trying to understand the MVC pattern, in particular the role of input fields in this pattern.

1. Conceptual question.
Let’s say I have a document as model and a text area displaying the document. Is the text area a view or a controller?

  • Because the text area shows the document (for example, after the user loads the document from disk), it is a view.
  • Because the user can interact with the text area (adding/removing/saving the text to disk), it is a controller.

2. Practical question.
Should changes in the document update the text area, or should changes in the text area update the document?

  • Changes in the document should update the text area, for example, when the document is changed on disk by an external program, or when the document is also synced with a different input field.
  • Changes in the text area should update the document, so that the user’s input is saved.

I was trying to do both via the observer pattern, and that creates an infinite loop (text area changes -> document updates -> text area changes …). How to break this cycle?

1

Is the text area a view or a controller?

The text area is a view, because it is a component the user interacts with. The view is the user interface. Of course this all depends on how complex this text area is.

If the text area is just a plain text input, then I would say it is part of the view.

If the text area is a rich text editor, then what you call a “text area” is an entire UI component that likely has its own set of sub controllers, sub views and sub models to make the user interaction work.

However, if you are not writing the code to implement a text area then it will be a component in the view, and is thus considered part of the view.

Because the user can interact with the text area (adding/removing/saving the text to disk), it is a controller.

A text area should not be loading or saving data from storage. That is what the controller is for.

Should changes in the document update the text area, or should changes in the text area update the document?

It depends.

For a traditional GUI application you could set up a process to monitor the document for changes, and auto update the document on screen, but then you need to deal with merging the document on disk with the changes the user applied to it. This is not an easy task. For this you usually warn the user that the document has changed, and give them the option to load the file again from disk.

When your application has a network sitting in between the file on disk and the text area, as you do with a web application or mobile app that utilizes web services, then it is extra work to set up a streaming web socket to broadcast these kinds of events from server to client. Generally when saving the document is when you check to see if it has changed, and warn the user if it has.

The reverse is also true. In a classic GUI application changes in the text area can easily propagate to the file on disk. This works well, until you have multiple people editing the same file.

In a web or mobile application, you can do the same thing, but you must also deal with the same pitfalls of modifying a shared resource.

In summary, if your application is using a text area, then it is part of the view.

If your application is literally the code required to implement a text area component that is embedded in other views, then it could have its own models, views and controllers.

Most people do not write their own text areas, and use pre-built components, so we consider text areas to be part of the view exclusively.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật