验证存储在observableArray

时间:2017-08-01 16:19:07

标签: knockout.js knockout-validation

我试图验证所有ko.validatedObservables。这就是我现在所拥有的,但它目前无法正常工作。我只想包装在createItem中创建的项目的属性,但这使得我无法访问我需要的那个项目的各个属性。迭代items数组中item的所有属性当前不起作用,但调用特定validatedObservable确实允许它调用isValid()函数。有任何想法吗?任何帮助,或者如果你能指出我正确的方向,我将不胜感激!

var items = ko.observableArray();


function createViewModel() {

    var createItem = function (setLineType, setWorkOrder) {
        var item = ({
            lineType: ko.validatedObservable(setLineType).extend({ required: true }),
            lineDescription: ko.validatedObservable(),
            workOrder: ko.validatedObservable(setWorkOrder),
            longDescription: ko.validatedObservable(),
            dropPoint: ko.validatedObservable(),
            commodityGroup: ko.validatedObservable(),
            chemical: ko.validatedObservable(),
            msdsRequired: ko.validatedObservable(),
            millTestRequired: ko.validatedObservable(),
            rebuildRepair: ko.validatedObservable(),
            quantity: ko.validatedObservable(),
            orderUnit: ko.validatedObservable(),
            unitCost: ko.validatedObservable(),
            serviceCost: ko.validatedObservable(),
            internalNotes: ko.validatedObservable(),
            catalogNum: ko.observable(),
            taxExempt: ko.observable()
        //        lineDescription: this.lineDescription.extend({
        //    maxLength: 50,
        //    required: true
        //}),
        //        workOrder: this.workOrder.extend({
        //    maxLength: 50,
        //    required: true
        //}),
        //        longDescription: this.longDescription.extend({
        //    maxLength: 150,
        //    required: true
        //}),
        //        dropPoint: this.dropPoint.extend({
        //    maxLength: 30
        //}),
        //        commodityGroup: this.commodityGroup.extend({
        //    maxLength: 30
        //}),
        //        quantity: this.quantity.extend({
        //    maxLength: 18
        //}),
        //        orderUnit: this.orderUnit.extend({
        //    maxLength: 20
        //}),
        //        unitCost: this.unitCost.extend({
        //    required: true
        //}),
        //        serviceCost: this.serviceCost.extend({
        //    required: true
        //}),
        //        internalNotes: this.internalNotes.extend({
        //    maxLength: 75
        //}),
        //        catalogNum: this.catalogNum.extend({
        //    maxLength: 15
        //}),
        //        requestType: this.requestType.extend({
        //    maxLength: 30
        //})
        });
        return item;
    };

    var addItem = function () {
        if (items().length > 0)
        {
            items.push(createItem(items()[0].lineType(), items()[0].workOrder()));
        } else {
            items.push(createItem("", ""));
        }           
    };

    var removeItem = function () {
        items.pop();
    };

    return {
        items: items,
        addItem: addItem,
        removeItem: removeItem
    };
}

function checkItems() {
    var validFlag = true;

    //for (var i = 0; i < items().length; i++)
    //{
    for (var property in items()[0]) {

        if (property.isValid()) {
            validFlag = false;
        }
    }
    //}

    return validFlag;
}

$(document).ready(function () {
    var viewModel = createViewModel();
    ko.applyBindings(viewModel);
});

0 个答案:

没有答案