Say you have a product overview page with the following view models:
- ProductIndexViewModel; the root view model that is bound to the entire page
- ProductListViewModel; a widget for displaying all products
- ProductRegistrationViewModel; a widget for registering new products
The widgets are loaded by using custom HTML elements (e.g. <product-list></product-list>
and <product-registration></product-registration>
). This is great because I don't have to put any knowledge of these widgets in my root-model. However, I would also like to refresh the product list after the user has registered a new product. In short:
How do I send a signal from ProductRegistrationViewModel to ProductListViewModel?
I've already looked into Knockout Postbox but this does not seem to solve the problem. What if I have multiple product lists and I only want to refresh one of them? Ideally, I would want to implement a series of public methods on my component's view model. Then tie the two together from my page's root view model, like this:
var ProductIndexViewModel = function()
{
var productRegistrationComponent = ??;
var productListComponent = ??;
productRegistrationComponent.onRegistrationComplete(function() {
productListComponent.refresh();
};
}
However, I don't have access to these view models from here. Or do I?
How can I access the child view-models from my root view model?
Finally, if anyone sees a better solution to my problem: I'm all ears!