最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Guidelines for models in MVVC pattern (specifically using knockout.js) - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Jan 29, 2012 at 20:39 Jeremy SmithJeremy Smith 15.1k19 gold badges70 silver badges115 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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 mon 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!

发布评论

评论列表(0)

  1. 暂无评论