Come aggiornare knockout.js Visualizza il modello con il server
-
12-12-2019 - |
Domanda
Ho il modello di visualizzazione Knockout.js che ha annidato array osservabili.
function ParentVM(data) {
var self = this;
self.childs= ko.observableArray([]);
ko.mapping.fromJS(data, mapping, this);
}
function ChildVM(data) {
var self = this;
self.propertyA = ko.observableArray([]);
self.propertyB = ko.observable();
ko.mapping.fromJS(data, mapping, this);
}
function GrandChildVM(data) {
var self = this;
self.propertyX = ko.observable();
self.propertyY = ko.observable();
self.propertyZ = ko.observable();
ko.mapping.fromJS(data, mapping, this);
}
.
Ho i seguenti attacchi:
data-bind="value: propertyX, valueUpdate: 'keydown'"
.
Ora, come posso aggiungere il gestore di eventi di chiamare il mio riposa Endpoint per aggiornare lo stato del modello visualizzazione quando queste proprietà cambiano?
Soluzione
Se ti capisco correttamente, questo è quello che puoi fare:
function ChildVM(data) {
var self = this;
self.propertyA = ko.observableArray([]);
self.propertyB = ko.observable();
ko.mapping.fromJS(data, mapping, this);
self.propertyA.subscribe(function (newValue) { /* call you're api with new value! */ });
self.propertyB.subscribe(function (newValue) { /* call you're api with new value! */ });
}
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow