Should there only be one controller per page within a JavaScript-based MVC architecture?

Let’s say that I create a page in my marketplace web application where buyers can see a catalog of all products offered by some or all sellers or sellers can see just their own products in a preview of this buyer’s view.

Within my JavaScript code I would create a model object that synced data with the server, one or more view objects that would render various pieces of that data to the user and a controller object that routed messages between the model and views.

Now let’s say I create another page in my application where a buyer can see a catalog of only the products offered by a single seller (yes, this is just a subset of the above functionality but just go with me on this for the sake of this hypothetical). In this case, though, the catalog exists on a tab on the page with sibling tabs that contain other unrelated data.

If I were to reuse all the objects I created for the first page, would it still be feasible to call the “central” piece a controller when it would be included in one tab of a page that would probably have its own controller object? Or would it be more proper to stick with a single controller per “page” and just call this some sort of view object even though its purpose remains to route messages between its own internal model and views?

The crux of my query is whether from a conceptual standpoint there should only be a single controller in the JavaScript code for a single page or whether abstracted component units can have their own MVC architecture hidden away from the page’s larger MVC architecture?

It really depends on the structure of the page. If a page has only one purpose and one concern, then you should try to use a single controller so that the code is grouped logically.

If there are multiple components on a page with different concerns, then you should absolutely use multiple controllers for the sake of decoupling.

For example, consider a page that has a body that tracks products by a seller and a header that tracks the user’s information and login session. The controller for the seller’s products should hold logic about prices, stock reporting, discounts and so forth. Giving that controller power over logging the user out, or changing their password, dilutes its purpose and makes it less reusable. On the other side, giving the user controller price information about the seller’s products is useless when you switch to a page about shipping arrival dates.

At the heart of it, this is just a higher-level version of the encapsulation discussions that go ’round and ’round the OOP world. One section of code, one purpose, no meddling without explicit permission from the code in question. This prevents code duplication, code corruption, and strange side-effects. It also makes the code much more maintainable when you only have to reason about a single topic when looking at a specific section of code.

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

Should there only be one controller per page within a JavaScript-based MVC architecture?

Let’s say that I create a page in my marketplace web application where buyers can see a catalog of all products offered by some or all sellers or sellers can see just their own products in a preview of this buyer’s view.

Within my JavaScript code I would create a model object that synced data with the server, one or more view objects that would render various pieces of that data to the user and a controller object that routed messages between the model and views.

Now let’s say I create another page in my application where a buyer can see a catalog of only the products offered by a single seller (yes, this is just a subset of the above functionality but just go with me on this for the sake of this hypothetical). In this case, though, the catalog exists on a tab on the page with sibling tabs that contain other unrelated data.

If I were to reuse all the objects I created for the first page, would it still be feasible to call the “central” piece a controller when it would be included in one tab of a page that would probably have its own controller object? Or would it be more proper to stick with a single controller per “page” and just call this some sort of view object even though its purpose remains to route messages between its own internal model and views?

The crux of my query is whether from a conceptual standpoint there should only be a single controller in the JavaScript code for a single page or whether abstracted component units can have their own MVC architecture hidden away from the page’s larger MVC architecture?

It really depends on the structure of the page. If a page has only one purpose and one concern, then you should try to use a single controller so that the code is grouped logically.

If there are multiple components on a page with different concerns, then you should absolutely use multiple controllers for the sake of decoupling.

For example, consider a page that has a body that tracks products by a seller and a header that tracks the user’s information and login session. The controller for the seller’s products should hold logic about prices, stock reporting, discounts and so forth. Giving that controller power over logging the user out, or changing their password, dilutes its purpose and makes it less reusable. On the other side, giving the user controller price information about the seller’s products is useless when you switch to a page about shipping arrival dates.

At the heart of it, this is just a higher-level version of the encapsulation discussions that go ’round and ’round the OOP world. One section of code, one purpose, no meddling without explicit permission from the code in question. This prevents code duplication, code corruption, and strange side-effects. It also makes the code much more maintainable when you only have to reason about a single topic when looking at a specific section of code.

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