I have a very dynamic site, that uses custom applications/widgets within the page and I was researching how I might improve the site with the help of Knockout.
- db <-namespace
- tabs[] -> tab
- id
- title
- type
- items[] -> widget
- id
- css
- privy
- widget-specific viewModel
- service
- tabs[] -> tab
If each ‘widget’ is a viewModel that exist within the same tab(div), what kind of issues would I come across?
Is it ok to have one viewModel that starts at the top and contains other models? or should I have individual models and just use ko.applyBindings(vm, element) to apply them?
We’ve had success both ways, but typically we’ll use the first scenario you mentioned, with one overall parent viewmodel containing instances of other, more specific, viewmodels. General practice suggests to avoid calling
applyBindingsfrequently. A quick mention of knockout’s ‘with’ binding seems appropriate in your instance: http://knockoutjs.com/documentation/with-binding.htmlThat would keep the binding expressions within your tabs more focused to the viewmodels that they’re representing:
Ultimately it’s entirely up to you how you’d like to structure your dom and view models, just laying out a common example we’ve found useful.