简体   繁体   English

调用ViewModel范围之外的函数

[英]Call a function outside of ViewModel scope

I have the following code outside the scope of my view model and i am attempting to call it with a click binding. 我在视图模型的范围之外有以下代码,并且我试图通过单击绑定来调用它。

function Policy(data) {
    var self = this;
    Object.keys(data).forEach(function(prop) {
        self[prop] = data[prop];
    });

    var generate = function() {
        console.log("test");
        // window.open("www.google.com")
        // window.open("{{ url_for('genreport') }}/" + qvm.letter() + '/' + self.id);
    }

}

however, when i try to call the function i get that generate is not defined. 但是,当我尝试调用该函数时,未定义生成的函数。 the code for my binding is below 我绑定的代码如下

<button class="btn btn-lg btn-primary btn-block" data-bind="click: function(){ generate() }">Generate</button>

I have tried calling Policy.generate, $data.generate, and i cannot get this function to call. 我尝试调用Policy.generate,$ data.generate,但无法调用此函数。

I know this issue is simple and im probably missing something that should be smashing me in the face but i'm oblivious, any help would be appreciated. 我知道这个问题很简单,我可能错过了一些应该让我大失所望的东西,但我忘了,任何帮助将不胜感激。

修复只是将上述功能放在我的ViewModel范围内,现在已修复。

You have declared generate using a local variable. 您已经使用局部变量声明了generate It is not visible outside the scope of Policy . Policy范围之外不可见。 If you want to create Policy.generate , you should do that: 如果要创建Policy.generate ,则应执行以下操作:

Policy.generate = function () {

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

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