2

我想从 AJAX JSON 条目更新 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>

Viewmodel 非常简单,我有一个货币 From 和一个要从表中添加和删除的货币。

4

2 回答 2

2

我会在这里做两件事。

首先,为 Currency 定义一个类。

var currency = function(data) {
  var self = this;
  self.CurrencyFrom = ko.observable(data.CurrencyFrom);
  self.CurrencyTo = ko.observable(data.CurrencyTo);
}

之后,你的成功方法就变成了这样。

success: function(Result) {
   // use jQuery's $.map function to translate values
   // should be stored in .d property, according to your JSON
   var mappedCurrencies = 
     $.map(Result.d, 
           // Here, $.map will just new up a new currency,
           // using the constructor argument to set fields
           function(item){ return new currency(item);});

   // Finally, set the currencies.  VM should update automatically.
   self.Currencies(mappedCurrencies);
}
于 2013-01-16T12:57:23.323 回答
0

我建议您将视图模型和数据上下文分开。为您的 ajax 请求开设课程是一种更好的做法

我假设您需要将数组“self.Currencies”与从服务接收的数据绑定,因此您只需在成功函数上执行此操作:

       success: function (Result) {
            ko.utils.arrayPushAll(self.Currencies, Result);
        }
于 2013-01-16T13:03:37.757 回答