淘汰赛不在document.ready下工作

时间:2014-06-10 00:56:30

标签: jquery knockout.js

我正在淘汰赛并遇到问题。下面是我的.js代码,而ko.applyBindings(viewModel);在document.ready下完全不起作用。它只适用于我添加

ko.applyBindings(viewModel);

在side方法$('#dtTable tbody')。on('click','tr',function()。如果我将代码置于数据表行中,请点击一下,如果我点击,请提出建议是什么问题第二次出现其他行或同一行错误“我们无法多次定义绑定”。 请告诉我代码中的问题和第二个应该在哪里提出申请绑定

/// <reference path="knockout-3.1.0.debug.js" />

$(document).ready(function () {     

    var viewModel = new function () {
        this.firstname = ko.observable();
        this.lastname = ko.observable();

    };


    $('#dTable').dataTable({
            "scrollY": 300,
            "scrollCollapse": true,
            "jQueryUI": true        
     });   

    var selectedRow;

    $('#dTable tbody').on('click', 'tr', function () {
        $(this).toggleClass('selected');
        selectedRow = $(this).closest('tr');

        viewModel.firstname = selectedRow.context.cells(0).innerHTML;
        viewModel.lastname = selectedRow.context.cells(1).innerHTML;        

    });  


    $('#btnSaveChanges').click(function () {      

        var saveRequest = new Object();

        saveRequest.FirstName = viewModel.firstname;
        saveRequest.LastName = viewModel.lastname; 


        $.ajax({
            async: true,
            type: 'POST',
            url: '/Person/GetPerson',
            data: JSON.stringify(saveRequest),
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function (result) {
                if (result == true) {

                    alert("Division saved successfully!");

                    if ($("#clickedButton").val() == "Add") {
                        AddNewRowToDataTable(saveRequest);
                    }
                    else {
                        UpdateRowToDataTable();
                    }

                }
                else {
                    alert("did not save!");
                }
            }
        });


    });

    var t = $('#dTable').DataTable();

    $('#btnAdd').on('click', function () {

        $("#clickedButton").val("Add");

        viewModel.firstname = "";
        viewModel.lastname = "";

    });

    function AddNewRowToDataTable(saveRequest) {
        t.row.add([
           saveRequest.FirstName,
           saveRequest.LastName,

        ]).draw();
    }

    function UpdateRowToDataTable() {
        selectedRow.context.cells(0).innerHTML = viewModel.firstname; 
        selectedRow.context.cells(1).innerHTML = viewModel.lastname; 

    }
    ko.applyBindings(viewModel);

});

感谢并等待回复

1 个答案:

答案 0 :(得分:1)

viewModel.firstnameviewModel.lastnames是可观察的。您可以像这样为它们分配值......

viewModel.firstname(selectedRow.context.cells(0).innerHTML);
viewModel.lastname(selectedRow.context.cells(1).innerHTML); 

当你使用赋值语句时,你实际上用其他东西覆盖了observable。第一次ko.applyBindings()运行时,视图仍然与可观察对象绑定,这就是为什么在您再次重新应用绑定之前,值不会发生变化(您不应该这样做)。< / p>

修正这些作业后,此代码会出现问题......

saveRequest.FirstName = viewModel.firstname;
saveRequest.LastName = viewModel.lastname; 

如果firstnamelastname是可观察的,则需要通过调用它们来访问它们的值...

saveRequest.FirstName = viewModel.firstname();
saveRequest.LastName = viewModel.lastname();