0

我一直在关注一个在线骨干验证的在线示例:

http://jsfiddle.net/thedersen/c3kK2/

到目前为止一切顺利,但现在我正在验证子视图并且它们不起作用。

我的代码如下所示:

var ScheduleModel = Backbone.Model.extend({
    validation: {
        StartDate: {
            required: true,
            fn: "isDate"
        },
        StartTime: [{
            required: true
        },
        {
            pattern: /^([0-2]\d):([0-5]\d)$/,
            msg: "Please provide a valid time in 24 hour format. ex: 23:45, 02:45"
        }],
        EndDate: {
            required: true,
            fn: "isDate"
        }
    },
    isDate: function (value, attr, computed) {
        if (isNaN(Date.parse(value))) {
            return "Is not a valid Date";
        }
    }
});
var ScheduleView = Backbone.View.extend({
    tagName: "div",
    template: _.template($("#scheduleAddTemplate").html()),
    render: function () {

        // append the template to the element
        this.$el.append(this.template(this.model.toJSON()));
        // set the schedule type
        var renderedInterval = SetScheduleType(this.model.attributes.ScheduleType.toLowerCase());
        // append to the interval
        $("#Interval", this.$el).append(renderedInterval.el);
        this.stickit();
        return this;
    },
    events: {

        "submit #NewScheduleForm": function (e) {
            e.preventDefault();
            if (this.model.isValid(true)) {
                this.model.save(null,
                {
                    success: function (schedule) {

                      //do stuff

                    }
                },
                { wait: true });
            }
        }
    },
    bindings: {
        "[name=ScheduleType]": {
            observe: "ScheduleType",
            setOptions: {
                validate: true
            }
        },
        "[name=StartDate]": {
            observe: "StartDate",
            setOptions: {
                validate: true
            }
        },
        "[name=StartTime]": {
            observe: "StartTime",
            setOptions: {
                validate: true
            }
        },
        "[name=EndDate]": {
            observe: "EndDate",
            setOptions: {
                validate: true
            }
        }
    },
    initialize: function () {
        Backbone.Validation.bind(this);
    },
    remove: function () {
        Backbone.Validation.unbind(this);
    }

});

我目前正在使用的可能时间间隔如下:

var MonthModel = Backbone.Model.extend({
    defaults: {
        DayOfMonth: 1,
        MonthsToSkip: 1
    },
    MonthsToSkip: {
        required: true,
        min: 1,
        msg: "Number must be greater than 1"
    },
    DayOfMonth: {
        required: function (val, attr, computed) {
            console.log(computed.ScheduleType);
            if (computed.ScheduleType === "monthly") {
                return true;
            }
            return false;
        }
    }
});
var MonthlyView = Backbone.View.extend({
    tagName: "div",
    attributes: function () {
        return { id: "Monthly", class: "inline co-xs-4" };
    },
    template: _.template($("#monthEditTemplate").html()),

    render: function () {
        // append the template to the element
        this.$el.append(this.template(this.model.toJSON()));
        this.stickit();
        return this;
    },
    bindings: {
        "[name=DayOfMonth]": {
            observe: "DayOfMonth",
            setOptions: {
                validate: true
            }
        },
        "[name=MonthsToSkip]": {
            observe: "MonthsToSkip",
            setOptions: {
                validate: true
            }
        }
    },
    initialize: function () {
        Backbone.Validation.bind(this);
    },
    remove: function () {
        Backbone.Validation.unbind(this);
    }
});

有谁知道为什么子视图没有验证?

4

1 回答 1

0

找到了方法。发布它是如何完成的,以防其他人发现这是一个问题。我只会展示相关的代码位,而不会显示所有绑定、初始化等。

var ScheduleView = Backbone.View.extend({
    render: function () {

        // this.Interval
        this.Interval = SetScheduleType(this.model.attributes.ScheduleType.toLowerCase(), this.model);
        // set the changed interval view
        $("#Interval", this.$el).append(this.Interval.render().el);

        this.stickit();
        return this;
    },

    events: {
        "change #NewScheduleForm": function (e) {
            // validate the subview when changes are made
            this.Interval.model.validate();
        },
        "change #ScheduleType": function (e) {
            e.preventDefault();
            var model = this.model;
            var newSchedType = e.target.value;
            this.model.attributes.ScheduleType = e.target.value;
            this.Interval = SetScheduleType(newSchedType, model);
            $("#Interval").html(this.Interval.render().el);
        },
        "submit #NewScheduleForm": function (e) {
            e.preventDefault();
            if ((this.model.isValid(true)) && (this.Interval.model.isValid(true))) {
                console.log("Success");
                this.model.save(null,
                {
                    success: function (schedule) {
                         //do stuff
                    }
                },
                { wait: true });
            }
        }
    }
    });

本质上,我将子视图变成了主视图上的一个属性。我在对主视图的任何更改和提交表单时手动调用子视图的验证。

于 2016-10-16T05:19:50.487 回答