Perform round-trip binding with 2 objects and knockoutjs

6

I would like to know if it is possible to make a binding between two objects ( viewmodel ), the first is created as part of the function and the other is created from the Get , and the property that you would like to synchronize between the two viewModels is visibleTabs with ultimoNivel with KnockoutJS.

My current code is as follows:

var vm = function() {
var self = this;
this.visibleTabs = ko.observable(false);
this.select = function(e) {
    var idEquipo = treeview().dataItem(e.node).id;
    var equipo = Get("equipo/getbyid/" + idEquipo).responseText;
    self.equipo(ko.mapping.fromJS(JSON.parse(equipo)));
    self.visibleTabs(self.equipo().ultimoNivel());
}
this.visibleTabsChange = ko.computed(function () {
    if (self.equipo()) {
        self.equipo().ultimoNivel(self.visibleTabs());
    }

});}
    
asked by yurivan 15.12.2015 в 18:57
source

1 answer

2

The solution I found was to change KnockouJS's computed for a subscribe of visibleTabsChange , also the next line self.visibleTabs(self.equipo().ultimoNivel()); was also moved to subscribe of the property equipo the code would be the following way:

var vm = function() {
var self = this;
this.visibleTabs = ko.observable(false);
this.select = function(e) {
    var idEquipo = treeview().dataItem(e.node).id;
    var equipo = Get("equipo/getbyid/" + idEquipo).responseText;
    self.equipo(ko.mapping.fromJS(JSON.parse(equipo)));
    self.visibleTabs(self.equipo().ultimoNivel());
}
this.visibleTabs.subscribe(function (newValue) {
    if (self.equipo()) {
        self.equipo().ultimoNivel(newValue);
    }
});
this.equipo.subscribe(function () {
    self.visibleTabs(self.equipo().ultimoNivel());
});}
    
answered by 15.12.2015 / 19:58
source