I am using Knock out to bind different sections in a page. I have two view model data for each section. While changing a element in one view model based on that want to change another element in another view Model.Each view model data have ID field to identify each data.
Asked
Active
Viewed 751 times
0
-
Check this out - http://stackoverflow.com/questions/9892124/whats-the-best-way-of-linking-synchronising-view-models-in-knockout – gkb Nov 28 '16 at 08:45
-
@gkb How to find by ID and update ? Same ID is used in both View Model – James Nov 28 '16 at 08:57
-
1I would suggest to provide some code that presents some specific task that you want to achieve.. – gkb Nov 28 '16 at 09:34
-
@gkb I have an ID field in both models which are same.When change a text field in on model I want to change the same ID text in another model. – James Nov 28 '16 at 09:43
-
@gkb 'postbox.subscribe(function (newValue) { self.text(newValue); }, this, "selected"); postbox.notifySubscribersself.text(), "selected");' – James Nov 28 '16 at 09:44
-
I have created this fiddle based on your comment above - https://jsfiddle.net/usxvqq9c/1/ ..have a look... – gkb Nov 28 '16 at 12:28
1 Answers
2
You can just pass one of the view model in to the other view model:
var ViewModel1 = function() {
var self = this;
self.selectedItem = ko.observable();
};
var ViewModel2 = function(viewModel) {
self = this;
self.content = ko.observable();
viewModel.selectedItem.subscribe(function(){
self.content('value changed')
});
};
var viewModel1 = new ViewModel1();
var viewModel2 = new ViewModel2(viewModel1);
ko.applyBindings(viewModel1, document.getElementById("part1"));
ko.applyBindings(viewModel2, document.getElementById("part2"));

Kevin Smith
- 13,746
- 4
- 52
- 77