将敲除验证应用于对象

时间:2015-09-03 20:01:48

标签: javascript validation knockout.js

我一直试图围绕如何将挖空验证应用于我的视图模型。在我发现的所有示例中,验证正在应用于视图模型中的各个属性。但是我想知道是否可以将验证应用于java脚本对象中的各个属性?这是我的视图模型代码:

$(document).ready(function () {
    BindViewModel();
});

function ManageCustomerViewModel() {

    var self = this;

    self.searchResults = ko.mapping.fromJS([]);
    self.customerCount = ko.observable();
    self.Customer = ko.observable();
    self.SearchOnKey = function (data, event) {
        if (event.keyCode == 13 || event.keyCode == 9) {
            self.CustomerSearch(data);
        }
        else {
            return true;
        }
    };

    self.AddCustomer = function () {

         var cust =  {
            Id: 0,
            FirstName: "",
            LastName: "",
            EmailAddress: "",
            AlternatePhone: "",
            BillingAddress: "",
            BillingCity: "",
            BillingState: "",
            BillingZip: "",
            PrimaryPhone: "",
            ShippingAddress: "",
            ShippingCity: "",
            ShippingState: "",
            ShippingZip: ""
         }
         self.Customer(cust);
    };

    self.UpdateCustomer = function () {
        ShowLoading();
        if (self.Customer().Id > 0) {
            $.ajax("../api/CustomerAPI/UpdateCustomer", {
                data: ko.mapping.toJSON(self.Customer),
                type: "post",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (e) {
                    HideLoading();
                    $('#Modal').modal('hide');
                }
            }).fail(function () { HideLoading() });
        }
        else {
            $.ajax("../api/CustomerAPI/InsertNewCustomer", {
                data: ko.mapping.toJSON(self.Customer),
                type: "post",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (e) {
                    HideLoading();
                    $('#Modal').modal('hide');
                }
            }).fail(function () { HideLoading() });
        }
    };

    self.GetCustomerDetails = function (c) {
        ShowLoading();
        $.getJSON("../api/CustomerAPI/GetCustomerByID", { id: c.Id }, function (cust) {
            self.Customer(cust);
            HideLoading();
        }).fail(function () { HideLoading() });
    }

    self.CustomerSearch = function (data) {

        var first = $("#txtFirstName").val();
        var last = $("#txtLastName").val();
        var email = $("#txtEmail").val();

        first = first.trim();
        last = last.trim();
        email = email.trim();

        if (first == "" && last == "" & email == "") {
            alert("Please enter at least one search value");
        }
        else {
            ShowLoading();
            $.getJSON("../api/CustomerAPI/GetCustomerSearchResults", { lastname: last, firstname: first, email: email }, function (allData) {
                ko.mapping.fromJS(allData, self.searchResults);
                self.customerCount(self.searchResults().length);
                HideLoading();
            }).fail(function () { HideLoading() });
        }
    };
}

function BindViewModel() {

    ko.applyBindings(new ManageCustomerViewModel());
};

如您所见,我使用Customer对象来回传递数据。我想在Customer的属性中添加验证。这是可行的还是我做错了?

0 个答案:

没有答案