値のリストにノックアウト データ バインド オプションを使用して「選択」を入力し、値の 1 つをデフォルトで「選択済み」に設定しようとしています。
2 つのサーバー要求があり、
- 値のリストを取得する (dataRepository.GetLifelines)
- リストからいずれかの値を [選択済み] に設定します。(dataRepository.GetMockSelectedLifeline)
最初の要件が解決されました。選択へのデータバインドは、「選択済み」値で正常に機能しています。
リストのデフォルトの「選択された値」の設定に問題があります。誰か助けてください。メソッドはthis.selectValueです。selectedLifeline を一致する「名前」に設定しようとしています。
function LifelineViewModel() {
this.lifelines = ko.observableArray([{}]);
this.selectedLifeline = ko.observable();
this.updateData = function (data) {
var boundUpdate = bind(function (value) {
this.lifelines.push(value);
}, this);
$.each(data, function (index, item) {
boundUpdate(item);
});
dataRepository.GetMockSelectedLifeline(bind(this.selectValue, this));
}
this.selectValue = function (data) {
this.selectedLifeline = ko.utils.arrayFirst(this.lifelines, function (lifeline) {
return lifeline.Name === data.Name;
});
}
}
LifelineViewModel.prototype.Init = function () {
var boundUpdateData = bind(this.updateData, this);
dataRepository.GetLifelines(boundUpdateData);
}
var bind = function (func, thisValue) {
return function () {
return func.apply(thisValue, arguments);
}
}