4

I'm working on web application design that includes Knockout.js and have an overall MVVM question: Does it make sense for the ViewModel to automatically inject a default HTML template (pulled from separate file)?

More detail: suppose I have a site like this...

header...
    widget 1
    widget 2
    widget 3
footer

...and widgets 1/2/3 are going to be Knockout.js ViewModels determined at runtime from an overall list of available widgets, each with associated HTML template file. I understand that in MVVM you want the view (HTML template in this case) to be separate from the ViewModel (Javascript file in this case) so that people can edit it separately and possibly provide multiple templates for different "skins". However, it seems like it would also make sense for the ViewModel to point to a default html template that gets automatically used unless the controlling code provides a different one.

Am I looking at this correctly?

As an example, see this answer on StackOverflow where he recommends injecting the HTML and then the ViewModel. Seems like a one-liner would make more sense in that case, with the possibility of overriding a default template value.

1
  • 1
    This is a fair question; but in my opinion there's no right answer. You could do either.
    – Jim G.
    Mar 19, 2013 at 6:18

2 Answers 2

1

MVVM doesn't have a strict definition, I use the one described by Microsoft (as understood by me, each one has their own ideas on it).

One of his main goal is to abstract the view from the viewmodel, and one of main rules is 'only views should call other views, because ViewModel should not know anything about his view'.

So to give a simple theoretical answer: I think when loaded, your view should ask the viewmodel the list of widgets to load and then your view should load the corresponding widgets.

This way, logic is handled by the ViewModel, and only a view calls other views.

0

I think in this context (of favouring flexibility via an interface comprised of widgets) using a ViewModel-assigned layout is fair game. i.e. the default interface/layout is just as well served as a template along with everything else. It just makes it harder to understand and you'd probably have another step or two in your workflow and debugging - though these could be engineered out with supporting apps or scripts.

Pros: flexibility, maintainability*

Cons: less intuitive*, slight delay in rendering

* if/unless you understand it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.