MVVC 模式模型指南(特别是使用 Knockout.js)

发布于 2024-12-29 19:33:57 字数 329 浏览 0 评论 0原文

这是一个一般性问题,但我希望得到一些反馈。我是 MVVC 范式的新手,我发现它非常适合我的 SPA,90% 都是以不同形式呈现数据(IE 很少有用户对数据的操作)。

但我对在模型和模型视图之间放置数据格式或“清理”的位置有点困惑。我的总体感觉是,我的模型视图应该只有与更新视图有关的淘汰赛或 jquery 特定代码。但某些格式似乎有点过于特定于视图,无法放入模型中。例如,如果我只是更改字符串以使它们更加“用户友好”,您会将其放入模型还是视图模型中?我似乎对于模型来说有太多以用户为中心/以页面为中心的知识,但对于视图模型来说却有太多领域知识。

我知道没有硬性规定,但只是想知道对于这种情况最好的指导方针是什么。

This is a general question, but I'd love some feedback. I'm new to working in the MVVC paradigm, and I'm finding it's a perfect fit for my SPA that is 90% about rendering data in different forms (IE very little user manipulation of the data).

But I'm a little confused on where to put formatting or "cleansing" of data between the model and the model-view. My general feeling is that my model-view should have only knockout or jquery specific code that is concerned with updating the view. But some of the formatting seems a little too view-specific to go in the model. For example, if I'm just changing strings to make them more "user friendly" would you put that in the model or the view-model? I'm seems like it's too much user-centric/page-centric knowledge for the model, and yet too much domain knowledge for the view-model.

I understand there's no hard and fast rules, but just wondering what the best guidelines are for situations like this.

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

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

发布评论

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

评论(1

呆° 2025-01-05 19:33:57

视图模型可以被认为是视图的模型。换句话说,它所支持的观点是非常具体的。虽然可以将多个视图绑定到同一视图模型,但更常见的是视图模型与其各自的视图之间存在一对一的关联。

为了回答你的问题,在视图模型中具有高度专业化、以视图为中心的行为并没有什么问题。这包括格式化逻辑。例如,您的模型可能会将价格显示为数值“23.34”。如果您的视图将其显示为货币,您的视图模型将调整此属性,以便将其格式化以供查看,“£23.34”。

最后,该模式是模型-视图-视图模型(MVVM),而不是 MVVC!

The view-model can be thought of as the model-of-the-view. In other words, it is quite specific to the view which it backs. Whilst it is possible to have multiple views bound to the same view model, it is much more common to have a one-to-one correlation between view-models and their respective view.

To answer your question, there is nothing wrong with having highly specialised, view-centric behaviour within the view-model. This includes formatting logic. For example, your model might expose a price as a numeric value, "23.34". If your view displays this as a currency, your view-model would adapt this property in order to format it for viewing, "£23.34".

Finally, the pattern is Model-View-ViewModel (MVVM), not MVVC!

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