Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
1  
This is a fair question; but in my opinion there's no right answer. You could do either. –  Jim G. Mar 19 '13 at 6:18
add comment

1 Answer

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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