Knockout Extended Observable在取消引用时返回undefined?

时间:2014-12-02 19:15:35

标签: javascript knockout.js

我在Javascript中有一个“对象”,它创建了一个扩展的可观察,位置等。

当我尝试使用值填充项目时,仅在Internet Explorer 11中出现错误,表示它是“无法对未定义或空引用的属性'Address1'。”

奇怪的是,当我进入控制台并键入“self”时,它将Location显示为可观察的。如果我输入“self.Location”,它会显示observable的功能。如果输入“self.Location()”,则表示未定义。几乎其他一切似乎都没问题。我包括两个可观察量的代码。一个(位置)导致问题。另一个参与者,在代码中似乎相似,但“被观察”就好了。

    self.Participants = ko.observableArray([]).extend({
        validation: [{
            validator: function (value) {
                if ((showConfig && showConfig.CreateWorkOrder) || (showConfig && showConfig.WorkOrderNumber) || (showConfig && showConfig.WorkOrderId)) {

                    self.ModelErrors({ sort: 7, element: "participants-container", error: "", clear: true });

                    return true;
                }
                var HasGroupRoute = self.GroupRoute() != "g0";
                if (HasGroupRoute) {
                    self.ModelErrors({ sort: 7, element: "participants-container", error: "", clear: true });

                    return true;
                }
                if ((!value || value.length <= 0) || HasGroupRoute) {
                    //$("#participants-container").addClass('sp-error');
                    //$("#divErrMsgBlock").show();
                    //addressError.push("<li style='text-align: left; list-style-type: square'>You must select a participant.</li>");
                    self.ModelErrors({ sort: 7, element: "participants-container", error: "<li style='text-align: left; list-style-type: square'>You must select a participant.</li>" });
                    return false;
                }
                else {
                    //$("#participants-container").removeClass('sp-error');
                    //$("#divErrMsgBlock").hide();
                    self.ModelErrors({ sort: 7, element: "participants-container", error: "", clear: true });
                    return true;
                }
            },
            message: '',

        }]

    });
    self.Location = ko.observable().extend({
        validation: [{
            validator: function (value) {
                if (value == undefined) return;                    
                var Address1 = (value.Address1() == undefined ? null : value.Address1());
                var City = (value.City() == undefined ? null : value.City()); 
                var State = (value.StateOrProvince() == undefined ? null : value.StateOrProvince()); 
                var Zip = (value.PostalCode() == undefined ? null : value.PostalCode());
                var _mustCheck = (self.AppointmentType() == "4" || self.JobReference() != undefined) || (Address1 != null && Address1 != '') || (Address1 != null && Address1 != '') || (City != null && City != '') || (Zip != null && Zip != '');
                var _missing = (Address1 == undefined || Address1 == null) || (City == undefined || City == null) || (State == undefined || State == null) || (Zip == undefined || Zip == null);

                if (_mustCheck && _missing) {
                    //addressError.push("<li style='text-align: left; list-style-type: square'>Address is not complete.</li>");
                    self.ModelErrors({ sort: 8, element: "location-container", error: "<li style='text-align: left; list-style-type: square'>Address is not complete.</li>" });

                    if (value.Address1() == null || value.Address1() == '') {
                        //$("#appt-address1").addClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "appt-address1", error: "", clear: false });
                    }
                    else {
                        //$("#appt-address1").removeClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "appt-address1", error: "", clear: true });
                    }

                    if (value.City() == null || value.City() == '') {
                        //$("#appt-city").addClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "appt-city", error: "", clear: false });
                    }
                    else {
                        //$("#appt-city").removeClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "appt-city", error: "", clear: true });
                    }

                    if (value.StateOrProvince() == undefined || value.StateOrProvince() == '') {
                        //$("#cmboStates").addClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "cmboStates", error: "", clear: false });
                    }
                    else {
                        //$("#cmboStates").removeClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "cmboStates", error: "", clear: true });
                    }

                    if (value.PostalCode() == null || value.PostalCode() == null) {
                        //$("#appt-zip").addClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "appt-zip", error: "", clear: false });
                    }
                    else {
                        //$("#appt-zip").removeClass('sp-error');
                        self.ModelErrors({ sort: 8, element: "appt-zip", error: "", clear: true });
                    }
                } else {
                    self.ModelErrors({ sort: 8, element: "location-container", error: "", clear: true });
                    self.ModelErrors({ sort: 8, element: "appt-address1", error: "", clear: true });
                    self.ModelErrors({ sort: 8, element: "appt-city", error: "", clear: true });
                    self.ModelErrors({ sort: 8, element: "cmboStates", error: "", clear: true });
                    self.ModelErrors({ sort: 8, element: "appt-zip", error: "", clear: true });
                }
            },
            message: ''
        }]
    });

在IE11中导致错误的行是:

 self.Location().Address1(model.Address1);

1 个答案:

答案 0 :(得分:0)

可观察的

self.Participants

使用空数组进行实例化,并且从一开始就具有值。

然而,

self.Location

未提供任何初始值,并且将初始化而没有导致未定义的值。也就是说,在调用之前的什么时候

self.Location().Address1(model.Address1);

您的代码是否提供了一个值来填充self.location observable?