دنبال کننده ها

۱۳۹۶ مرداد ۱۰, سه‌شنبه

knockout.js - Validate all Knockout validatedObservables stored in observableArray

[ad_1]



I'm trying to validate all the ko.validatedObservables. This is what I have right now but it currently is not working. I would just wrap the properties for the item created in createItem but this makes it so that I cannot access the individual properties of that item which I need. Iterating through all the properties of item in items array currently doesn't work but calling the specific validatedObservable does allow it to call the isValid() function. Any ideas? Any help or if you could point me in the right direction it would be much appreciated!



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);
);



[ad_2]

لینک منبع