From the viewmodel I want to update value to the server straight after it was changed in the view.
class OrderLine
{
itemCode: KnockoutObservable<string>;
itemName: KnockoutObservable<string>;
constructor(code: string, name: string)
{
this.itemCode = ko.observable(code);
this.itemName = ko.observable(code);
//subscribers
this.itemCode.subscribe(this.updateCode, this, "change");
this.itemName.subscribe(this.updateName, this, "change");
}
updateCode = (newvalue: string) =>
{
//Update value to the server
}
updateName = (newvalue: string) =>
{
//Update value to the server
}
}
Both values can be changed by user and with explicit subscriptions updating to the server/database works fine.
In the server side when updating itemCode
will update value of itemName
too. So response to the client will return json
object with new value for itemName
Here i have a problem, because changing value of itemName
in the viewmodel will fire a subscription callback method which will update same value to the server again
updateCode = (newvalue: string) =>
{
//Update value to the server
//in the handler of the successful request
this.itemName(updatedvaluefromServer); //-- this trigger another request
}
Question: is it possible to change value of KnockoutObservable
which will notify only view subscribers?
Or is it possible to detect that value was changed from the view?
I tried used "sneaky update" by @RPNiemeyer from here https://stackoverflow.com/a/17984353/1565525
but this approach will suspend all subscribers from being notified, including view's subscribers