Reputation: 8640
I've run into this pattern several times now and haven't found an elegant way to handle it. I'm trying to set up a page with a read-only and edit mode.
Here is the html:
<div>
<span data-bind="text: EventType, if: !EditMode()"></span>
<select data-bind="options: $root.EventTypes, optionsValue: 'Id',
optionsText: 'Name', value: EventType, if: EditMode()"></select>
</div>
<div>
<a href="#" data-bind="click: edit">Edit</a>
</div>
And the javascript:
function viewModel(eventJSON) {
var self = this;
self.EventType = ko.observable(eventJSON.EventType);
self.EventTypes = ko.observableArray([]);
self.edit = function() {
$.getJSON("url...", function(eventTypesJSON) {
self.EventTypes(eventTypesJSON);
}
}
}
I'm running into a timing issue with the databinding. When applyBindings is called, the span gets populated correctly. However, when I enter edit mode, the drop down list is not set to the selected value and EventType is set to undefined.
Here's what I think is happening:
What I'd like it to do:
Upvotes: 0
Views: 2242
Reputation: 1072
I believe I had the same problem today and found a solution for it:
I was able to make it work by passing the selected value in to the function that loads the array from a web service call. After the call has been completed, I assign the value back to the observable originally used as the selected value. Doing so forces the subscribers to that field to re-bind so the SELECT that is now filled correctly changes the selected value.
I know this is late for you and may not be exactly your situation but it sounds similar and might help someone else.
Upvotes: 2
Reputation: 1638
Instead of setting up the EventTypes array as empty, bootstrap it with the event type that's initially selected, so it won't clear the EventType value when the select is enabled. The content will be overwritten anyway once the ajax returns from the call. i.e.:
self.EventTypes = ko.observableArray([eventJSON.EventType]);
Alternatively, if for some reason you can't initialize the array beforehand, and since the initial event is still in scope of the ajax callback, simply reset EventType just after loading the array:
$.getJSON("url...", function(eventTypesJSON) {
self.EventTypes(eventTypesJSON);
self.EventType(eventJson.EventType);
}
In the latter, just be careful not to use the same name for the initial view model parameter and the one in the ajax callback, or the latter will overwrite the first.
Upvotes: 1