How do I trigger an update to child elements when a parent observable changes using KnockoutJs?
In my application, I'm building a translation tool. I have a knockout class that represents the raw (default) value of some text, with a collection of translated children:
function ParentObject(id, defaultValue) {
var self = this;
self.id = id;
self.defaultValue = ko.observable(defaultValue);
self.children = ko.observableArray();
self.loadChildren = function () {
// standard code to load the children ("lazy load", as I have gobs of data)
}
}
And the child is
function Child(id, translation, cultureCode) {
var self = this;
self.id = id;
self.cultureCode = cultureCode;
self.translation= ko.observable(translation);
}
The parent item's defaultValue property is bound to an input control.
What I want to do is call my translation service for each child when I update the default value of the parent. But I'm a little lost as to how to proceed.
- How do I recognize that the parent's "defaultValue" property has changed?
- Should I iterate the children in the parent when that happens, or somehow move that to the child as well?
(note, my example is simplified from the real implementation)
EDIT: added this to my defaultValue element along with a function, still passes old value:
data-bind=" value: defaultValue, event: {change: updateChildren}"
where updateChildren iterates the child array.