在真正的 MVC(桌面)应用程序中单例视图有哪些好处?

发布于 2024-10-22 18:29:35 字数 653 浏览 6 评论 0原文

我是构建单页 Javascript 应用程序的团队的成员。我主要有 Web 背景(Struts、Spring、Rails、Sinatra 等),我知道它们对 MVC 模式有不同的解释。我在 Desktop MVC(我相信它适用于单页 Javascript 应用程序)中看到的一件事是它们使用可实例化模型以及单例控制器和视图。也就是说,可能有 Company.BookCompany.recentlyPurchasedBooksControllerCompany.recentlyPurchasedBooksView。控制器保留模型(或其集合)的实例,并且视图随着模型的更改而自行更新。

我的团队(也来自网络背景)发现这非常“神奇”。他们强烈喜欢这样的工作流程,其中控制器创建视图的实例,用模型填充它,并将其呈现到页面的一部分。

我对此有一些反对意见,但我没有任何个人经验告诉我我的反对意见实际上重要。首先,控制器不应该了解 DOM,因此无法告诉视图在哪里渲染。他们认为视图应该是可实例化的(“如果你想要页面上视图的多个副本怎么办?”),我对此回应说,在这些情况下他们可能需要一个集合视图。

我在哪里可以找到博客文章、书籍、参考资料以及任何有助于解决这场争论的东西?从长远来看,为了开发人员的舒适度而打破 MVC 模式会带来净收益还是净损失?

I'm a member of a team building a single-page Javascript application. I come largely from a web background (Struts, Spring, Rails, Sinatra, etc.), which I know has a different interpretation of the MVC pattern. One thing I've seen in Desktop MVC (which I believe applies to single-page Javascript applications) is that they use instantiable models and singleton controllers and views. That is, there might be Company.Book, Company.recentlyPurchasedBooksController, and Company.recentlyPurchasedBooksView. The controller keeps an instance of the model (or collection thereof) and the view updates itself as that model changes.

My team -- also being from a web background -- find this very "magical." They strongly prefer a workflow wherein the controller creates an instance of the view, populates it with the model, and renders it to a portion of the page.

I have a few objections with this, but I don't have any personal experience telling me that my objections actually matter. The first is that controllers shouldn't know about the DOM, and thus can't tell the view where to render. They argue that views should be instantiable ("what if you want multiple copies of the view on the page?"), to which I respond that they probably want a collection view in those cases.

Where can I find blog posts, books, references, anything that will help settle this debate? Will breaking the MVC pattern for the sake of developer comfort be a net-gain or net-loss over the long term?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-10-29 18:29:35

虽然我自己不使用它*,puremvc 有一个 MVC 实现的 javascript 端口,我发现它非常有启发性仔细看看。很明显的关注点分离,包括你提到的控制器视图知识,很漂亮,就像它们的对象图一样。

*对于一页 javascript Web 和 Air 应用程序,我一直在使用 JavaScriptMVC 我发现它可以缓解我的开发问题end(虽然我没有使用全部,只使用我喜欢的部分 - VC)。

Although I don't use it myself*, puremvc has a javascript port of its MVC implementation which I have found very instructive to poke around in. It's clear separation of concerns, including the one you mention, controller-view knowledge, is beautiful, as is their object diagram.

*For one page javascript web and air apps I have been using JavaScriptMVC which I have found to ease my development problems no end (although I don't use all of it, just the parts I like - VC).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文