0

我有一个可观察的可观察数组 - 每个可观察的数组都已针对要求进行了扩展,但是当我保存带有清除字段的表单时,它不会收集错误或显示任何错误 -

ko.validation.configure({
    grouping: {
        deep: true,
        observable: true
    }
});


var viewModel = function () {
    var self = this;
    var data = {
        "deadzone": "0.2",
            "sloperange": "2",
            "stepsize": "0.8",
            "setpoint": "0.75",
            "pressure": "1"
    };
    self.defaultCalculations = ko.observableArray([]);
    self.defaultCalculationProperties = function (defaults) {
        var self = this;
        var properties = defaults;
        self.deadzone = ko.observable(properties.deadzone || '').extend({
            required: {
                message: '*required'
            }
        });
        self.sloperange = ko.observable(properties.sloperange || '').extend({
            required: {
                message: '*required'
            }
        });
        self.stepsize = ko.observable(properties.stepsize || '').extend({
            required: {
                message: '*required'
            }
        });
        self.setpoint = ko.observable(properties.setpoint || '').extend({
            required: {
                message: '*required'
            }
        });
        self.pressure = ko.observable(properties.pressure || '').extend({
            required: {
                message: '*required'
            }
        });

    };
    //populate calculation values
    self.populateCalculationDefaults = function (data) {

        //pushing data to an array and mapping each item
        var temp = [];
        temp[0] = data;
        var mappedTasks = _.map(temp, function (item) {
            return new self.defaultCalculationProperties(item);
        });

        self.defaultCalculations(mappedTasks);
    };

    self.save = function (model) {
        //console.log(model);
        console.log(self.errors().length, self.errors.showAllMessages());
        if (self.errors().length != 0) {
            self.errors.showAllMessages();
        }
    };
    self.errors = ko.validation.group(self.defaultCalculations());

    self.populateCalculationDefaults(data);
};

ko.applyBindings(new viewModel());

不确定问题是什么,并且已经搜索过 stackflow - 有什么建议吗?

http://jsfiddle.net/kmcadams/W2SZb/

4

2 回答 2

1

最简单的方法是在 populate 函数中再次调用 ko.validation.group。我更喜欢的另一个选择是订阅默认计算并在那里运行群呼。

self.defaultCalculations.subscribe(function () {
    self.errors = ko.validation.group(self.defaultCalculations);
});

这将手动订阅要在 defaultCalculations 添加/删除项目时运行的函数。

于 2013-05-29T01:13:59.213 回答
0

您还可以在扩展 observable 时始终添加通知。

this.validationModel = ko.validatedObservable({
    Num: ko.observable(this.Num).extend({ number: true, notify: 'always' }),
});

它对我有用。之前,当 this.Num 没有被修改时,它没有被验证。

于 2014-03-21T10:59:25.770 回答