AJAXJSONエントリからKnockoutJSビューモデルを更新したい。これを行う方法がわかりません。
これが私のコードです:
var CurrencyModel = function (Currencies) {
var self = this;
self.Currencies = ko.observableArray(Currencies);
self.AddCurrency = function () {
self.Currencies.push({
CurrencyFrom: "",
CurrencyTo: ""
});
};
self.RemoveCurrency = function (Currency) {
self.Currencies.remove(Currency);
};
self.Save = function (Form) {
alert("Could Now Save: " + ko.utils.stringifyJson(self.Currencies));
};
$.ajax({
url: "CurrencyConfiguration.aspx/GetConfiguredCurrencies",
// Current Page, Method
data: '{}',
// parameter map as JSON
type: "POST",
// data has to be POSTed
contentType: "application/json; charset=utf-8",
// posting JSON content
dataType: "JSON",
// type of data is JSON (must be upper case!)
timeout: 10000,
// AJAX timeout
success: function (Result) {
//Need to Get method to Bind To CurrencyModel;
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
};
$(document).ready(function () {
var VM = new CurrencyModel();
ko.applyBindings(VM);
})
サーバーから取得したJSONデータは次のとおりです。
{
"d": [
{
"__type": "Finance.Tracntrace.Members_Only.DAL.DataModel.Currency.CurrencyConfigurationDM",
"CurrencyFrom": "ZAR",
"CurrencyTo": "USD"
},
{
"__type": "Finance.Tracntrace.Members_Only.DAL.DataModel.Currency.CurrencyConfigurationDM",
"CurrencyFrom": "USD",
"CurrencyTo": "ZAR"
}
]
}
HTML:
<table class="table table-striped">
<thead>
<tr>
<th>
Currency From
</th>
<th>
Currency To
</th>
</tr>
</thead>
<tbody data-bind="foreach: Currencies">
<tr>
<td data-bind="text: CurrencyFrom">
</td>
<td data-bind="text: CurrencyTo">
</td>
</tr>
</tbody>
</table>
ビューモデルは非常にシンプルで、通貨Fromと、テーブルに追加およびテーブルから削除したい通貨があります。