我有一个带功能的viewModel

 function resetForm(){
        loanSystem("lis");
        status("");
        processor("");
        fileType("funded");
        orderDate("");
        loanNumber("");
        team("");
        borrower("");
        tracking("");
    };


和页面上的按钮

 <button type="reset" data-bind="click: resetForm">Reset</button>


但是我得到了错误:


  无法处理绑定“单击:function(){return resetForm}。未定义resetForm。


但是我知道我的resetForm()可以工作,因为它在我的init function中被调用,如果我更改resetForm()中的值,它会在初始页面加载时显示。有任何想法吗?



var homeViewModel = (function () {
    var loanSystem = ko.observable("lis");
    var status = ko.observable("");
    var processor = ko.observable("");
    var fileType = ko.observable("funded");
    var orderDate = ko.observable("");
    var loanNumber = ko.observable("");
    var team = ko.observable("");
    var borrower = ko.observable("");
    var tracking = ko.observable("");
    var isLoanSystem = ko.computed(function () {
        return (loanSystem() == "lps");
    });
    var isCalendar = ko.computed(function () {
        return (isLoanSystem() && fileType() == "cancelled");
    });

    function resetForm(){
        loanSystem("lis");
        status("");
        processor("");
        fileType("funded");
        orderDate("");
        loanNumber("");
        team("");
        borrower("");
        tracking("");
    };

    function submitFileInformation() {
        if (!ValidInputs()) {
            return;
        }
        var fileData = {
            loanSystem: loanSystem(),
            status: status(),
            processor: processor(),
            fileType: fileType(),
            orderDate: orderDate(),
            loanNumber: loanNumber(),
            team: team(),
            borrower: borrower(),
            tracking: tracking()
        };

        $.ajax({
            type: "POST",
            url: "/ComplianceProcessing/Ajax.ashx",
            data: JSON.stringify(fileData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                if (data === false) {
                    alert("There was an error writing the file to the database.");
                    return;
                }
                alert('Data has been successfully saved.');
                resetForm();
            },
            failure: function() {
                alert('Failure');
            }
        });
    }

    return {
            init: function() {
                resetForm();
            },
            loanSystem: loanSystem,
            isLoanSystem: isLoanSystem,
            status: status,
            processor: processor,
            fileType: fileType,
            isCalendar: isCalendar,
            orderDate:orderDate,
            loanNumber: loanNumber,
            team: team,
            borrower: borrower,
            tracking: tracking
        };
    })(); 

最佳答案

您的resetForm不会从您的视图模型中删除,这使得它本质上是私有的,因此淘汰赛无法找到它。

该解决方案非常简单,只需在返回的对象(即您的“公共API”)中包含resetForm

return {
            init: function() {
                resetForm();
            },
            loanSystem: loanSystem,
            isLoanSystem: isLoanSystem,
            status: status,
            processor: processor,
            fileType: fileType,
            isCalendar: isCalendar,
            orderDate:orderDate,
            loanNumber: loanNumber,
            team: team,
            borrower: borrower,
            tracking: tracking,
            resetForm: resetForm //add this line
        };

09-26 10:28