简体   繁体   English

取消引用后,敲除扩展Observable返回未定义?

[英]Knockout Extended Observable returning undefined when dereferenced?

I have an "object" in Javascript that creates an extended observable, Location, among others. 我在Javascript中有一个“对象”,可以创建一个扩展的可观察位置(Location)等等。

When I try to populate the item with values, in Internet Explorer 11 only it seems, an error is thrown saying it is "Unable to the property 'Address1' of undefined or null reference." 当我尝试用值填充项目时,似乎只有在Internet Explorer 11中,会引发错误,指出它“无法使用未定义或空引用的属性'Address1'”。

What is weird is when I go into console and type out "self", it shows Location as an observable. 奇怪的是,当我进入控制台并键入“ self”时,它显示“ Location”为可观察到的位置。 If i type "self.Location", it shows the function of the observable. 如果我键入“ self.Location”,则显示可观察的功能。 If it type "self.Location()", it says undefined. 如果键入“ self.Location()”,则表示未定义。 Almost everything else seems to be ok. 几乎所有其他一切似乎都还可以。 I am including the code for two of the observables. 我包括了两个可观察对象的代码。 One (Location) which causes the problem. 一个(位置)导致问题。 The other, Participants, that seems similar in code, but is "observed" just fine. 另一个(参与者)在代码上看起来相似,但是“观察到”就可以了。

    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: ''
        }]
    });

The line causing the error in IE11 is this: 导致IE11错误的行是这样的:

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

The observable 可观察的

self.Participants

is instantiated with an empty array and has a value from the very beginning. 用一个空数组实例化,并从一开始就具有一个值。

However, 然而,

self.Location

is not supplied any initial value and will initialize without a value resulting in undefined. 没有提供任何初始值,并且将初始化而没有导致未定义的值。 That said, at what point prior to calling 也就是说,在致电之前

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

does your code supply a value to populate the self.location observable? 您的代码是否提供了一个值来填充可观察的self.location?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM