Jeremy Smith
Jeremy Smith

Reputation: 15069

Guidelines for models in MVVC pattern (specifically using knockout.js)

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.

Upvotes: 1

Views: 4552

Answers (1)

ColinE
ColinE

Reputation: 70122

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!

Upvotes: 7

Related Questions