I'm trying to modularize my code and as a result have a situation where I want to pass an observable from one view model into a second and have that observable bound in the context of the second view model.
Roughly what I have is:
var ModelA = function () {
var self=this;
self.model_b = new ModelB();
self.my_variable = ko.observable();
self.model_b(self.my_observable);
};
var ModelB = function () {
var self=this;
function passObservable( passed_observable ) {
self.received_variable = observable;
}
};
ko.applyBindings(new ModelA());
Then in the view, I want to bind using data-bind='text: model_b.received_variable'
========== FOLLOW UP ==========
One suggested solution was to have two independent observables, one in each viewmodel and then use the gang of four observer pattern to sync up these two observables, which can be implemented using KO's subscribable object. It feels unnecessarily heavy to have two observables when I only want one and, then to need to add the code to let these two objects communicate with one an other.