简体   繁体   English

Javascript & knockoutjs:如何重构以下代码以便能够访问 function 之外的属性

[英]Javascript & knockoutjs: how to refactor the following code to be able to access the properties outside the function

Im struggling to find a way to get the properties Override & Justification available outside of the function.我正在努力寻找一种方法来获取 function 之外可用的属性 Override & Justification。 The code is:代码是:

 self.CasOverridesViewModel = ko.observable(self.CasOverridesViewModel);

 var hasOverrides = typeof self.CasOverridesViewModel === typeof(Function);

    if (hasOverrides) {
        self.setupOverrides = function() {
            var extendViewModel = function(obj, extend) {
                for (var property in obj) {
                    if (obj.hasOwnProperty(property)) {
                        extend(obj[property]);
                    }
                }
            };

            extendViewModel(self.CasOverridesViewModel(), function(item) {

                item.isOverrideFilledIn = ko.computed( function() {
                    var result = false;

                    if (!!item.Override()) {
                        result = true;
                    }

                    return result;
                });

                if (item) {
                    item.isJustificationMissing = ko.computed(function() {
                        var override = item.Override();
                        var result = false;
                        
                        if (!!override) {
                            result = !item.hasAtleastNineWords();
                        }

                        return result;
                    });

                    item.hasAtleastNineWords = ko.computed(function() {
                        var justification = item.Justification(),
                            moreThanNineWords = false;

                        if (justification != null) {
                            moreThanNineWords = justification.trim().split(/\s+/).length > 9;
                        } 

                        return moreThanNineWords;
                    });

                    item.isValid = ko.computed(function() {
                        return (!item.isJustificationMissing());
                    });
                }
            });
        }();
    }

I've tried it by setting up a global variable like:我已经通过设置一个全局变量来尝试它:

var item;
or
var obj;

if(hasOverrides) {...


So the thing that gets me the most that im not able to grasp how the connection is made between the underlying model CasOverridesviewModel.因此,最让我无法理解的是如何在底层 model CasOverridesviewModel 之间建立连接。 As i assumed that self.CasOverridesViewModel.Override() would be able to fetch the data that is written on the screen.正如我假设 self.CasOverridesViewModel.Override() 将能够获取写入屏幕上的数据。

Another try i did was var override = ko.observable(self.CasOverridesViewModel.Override()), which led to js typeError as you cannot read from an undefined object.我做的另一个尝试是 var override = ko.observable(self.CasOverridesViewModel.Override()),这导致了 js typeError,因为您无法从未定义的 object 中读取。

So if anyone is able to give me some guidance on how to get the fields from an input field available outside of this function.因此,如果有人能够就如何从 function 之外的可用输入字段中获取字段提供一些指导。 It would be deeply appreciated.将不胜感激。 If I need to clarify some aspects do not hesitate to ask.如果我需要澄清某些方面,请不要犹豫。

The upmost gratitude!致以最诚挚的谢意!

not sure how far outside you wanted to go with your variable but if you just define your global var at root level but only add to it at the moment your inner variable gets a value, you won't get the error of setting undefined.不知道你想用你的变量在 go 之外多远,但是如果你只是在根级别定义你的全局变量,但只是在你的内部变量获得值时才添加到它,你不会得到设置未定义的错误。

 var root = { override: ko.observable() }; root.override.subscribe((val) => console.log(val)); var ViewModel = function () { var self = this; self.override = ko.observable(); self.override.subscribe((val) => root.override(val)); self.load = function () { self.override(true); }; self.load(); }; ko.applyBindings(new ViewModel());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

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

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