如何在ViewModel的内部函数中访问可观察到的外部函数? [英] How to access outer function observable in inner function in viewmodel?

查看:66
本文介绍了如何在ViewModel的内部函数中访问可观察到的外部函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我里面有一个主函数和子函数,

var main= function () {
    var self = this;
    self.value1 = ko.observable("");
     var data =   self.value1();   
     self.revaluate = ko.computed(function(){ 
     data = self.self.value1(); // i am overwriting 
     });

    function inner(i1,i2)
    {
        var self= this ;

        self.id1=ko.observable(i1);
        self.id2=ko.observable(i2);
        self.value2 = ko.observable("");

        self.visibility = ko.computed(function() {
            if (data == 1) {return true;}
            else {false;}
        });
    }
}

ko.applyBindings(new main());

我需要在我的inner函数内的value1值应该是动态的.

我尝试将其存储在变量中并对其进行访问,但是由于在某些情况下self.visibility根本无法触发并动态更新,因此无法解决我的情况.

加载时,我得到了更新的value1,因此一切正常,但是我有一个按钮转到下一个阶段",单击该按钮时,我正在更新状态,即self.value1(2).此时self.visibility应该随value1中的变化而更新.

我试图在一个简单的示例中展示我的需求,但实际上,正在进行一些更复杂的循环.

任何线索都会很好.

解决方案

要使计算结果在self.value1更新时进行更新,您实际上必须在计算函数内部使用它

self.visibility = ko.computed(function(){
        if( self.value1() == 1)
            {return true;}
        else
           { return false;}
         });
        }

您的计算不起作用的主要原因是因为您使用 data 来计算这是一个简单的JS变量,这就是为什么它仅在加载时更新的原因, 数据 从未真正进行过重新评估,因此您的计算结果从未得到过更新.您需要使用的是 ko.observable 以便计算正确更新!

另一个注意事项:我看不到您在计算对象中使用了内部函数中的任何变量.如果在您的实际示例中是这种情况,那么您可以简单地将已计算的结果转移到主函数中.

由于我没有有效的代码,也不知道您如何使用此内部函数,因此我无法创建有效的示例,只是指出了问题. self.value1会遇到范围问题,但是您应该能够克服这个问题,尝试将其作为变量传递,使用全局变量...无论哪种方法适合您

I am having a main function and sub function inside that, like this:

var main= function () {
    var self = this;
    self.value1 = ko.observable("");
     var data =   self.value1();   
     self.revaluate = ko.computed(function(){ 
     data = self.self.value1(); // i am overwriting 
     });

    function inner(i1,i2)
    {
        var self= this ;

        self.id1=ko.observable(i1);
        self.id2=ko.observable(i2);
        self.value2 = ko.observable("");

        self.visibility = ko.computed(function() {
            if (data == 1) {return true;}
            else {false;}
        });
    }
}

ko.applyBindings(new main());

I need the value1 value inside my inner function that should be dynamic.

I tried storing it in a variable and accessing it, but it's not gonna solve my case as there is a scenario where self.visibility won't fire at all and update dynamically.

Onload I get an updated value1 so everything will work, but I have a button "goto next stage", and onclick of that button I am updating the status, i.e self.value1(2). At this point self.visibility should update as there is change in value1.

I tried to show what I need in a simple example, but in reality there's some more complex looping going on.

Any clue will be great.

解决方案

For your computed to update when self.value1 updates you actually have to use it inside computed function

self.visibility = ko.computed(function(){
        if( self.value1() == 1)
            {return true;}
        else
           { return false;}
         });
        }

Main reason why your computed didn't work is because you were using data to calculate the computed which is a simple JS variable, that is why it updated only onload, data never actually reevaluated, so your computed never got updated. What you need to use is an ko.observable for computed to update properly!

One other note: I don't see that you've used any variable from inner function in your computed. If that is the case in your real example than you can simply transfer the computed to main function.

Edit: As I don't have a working code, and don't know how are you using this inner function I can't create a working example just point to problems. self.value1 will have scope problems but you should be able to overcome this, try passing it as a variable, use global variable... Whichever works for you

这篇关于如何在ViewModel的内部函数中访问可观察到的外部函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆