weixin_33725722 2015-04-28 12:46 采纳率: 0%
浏览 61

淘汰赛ViewModel更新

I am new to Knockout and I am trying to update my ViewModel from an ajax call.
This is what I have right now:

LoanDeductions.js

var deductionLine = function (deductionID, deductionName, amount) {
    self = this;
    self.deductionID = ko.observable(deductionID);
    self.deductionName = ko.observable(deductionName);
    self.amount = ko.observable(amount);
};

function LoanDeductions(deductions) {
    var self = this;
    self.loanDeductions = ko.observableArray(ko.utils.arrayMap(deductions, function (deduction) {
        return new deductionLine(deduction.deductionID, deduction.deductionName, deduction.amount)
    }));

    // Operationss
    self.removeLine = function (line) { self.loanDeductions.remove(line) };
};

and this is my scripts in my view:

    @section scripts
    {
        <script src="~/Scripts/koModel/LoanDeductions.js"></script>
        <script type="text/javascript">
           var updateValues = function () {
               $.ajax({
               'url': '@Url.Action("UpdateDeductionValues","LoanApp")',
               'data': { amount: $('.LoanAmount').val() },
               'success': function (result) {// update viewmodel scripts here}
            });
            var viewModel = new LoanDeductions(@Html.Raw(Model.CRefLoansDeductions2.ToJson()));
            $(document).ready(function () {
                ko.applyBindings(viewModel);
                $('.datepicker').datepicker();
                $('.LoanAmount').change(function () {
                   updateValues();
                };
            });
        });
    </script>
    }

So, in my view, I have a dropdown list with class name "LoanAmount" which when value is changed, it will perform an ajax call, send the selected loanAmount value to the server, recompute the deduction amounts, then the server returns a jsonresult that looks like this:

"[{\"deductionID\":1,\"deductionName\":\"Loan Redemption Fee\",\"amount\":53.10},{\"deductionID\":2,\"deductionName\":\"Document Stamp\",\"amount\":9.00}]"

Now what I wanted to do is use this json data as my new viewModel.
Can anyone show me the way how to do this, please note that I manually mapped my viewmodel and didn't used the ko mapping plugin.
Any help will be greatly appreciated. Thank you, more power!

EDIT (in response to Fabio)

function updateData() {
        $.ajax({
            url: '@Url.Action("UpdateDeductionValues","LoanApp")',
            data: { amount: self.selectedLoanAmount() },
            success: function (deductions) {
                //load your array with ko.utils.arrayMap
                ko.utils.arrayMap(deductions, function (deduction) {
                return new deductionLine(deduction.deductionID, deduction.deductionName, deduction.amount)
                });
            }
        });
    }
  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 如何降低hdfs中datanode的JVM内存用量
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver