传递一个返回ko.computed值的函数 [英] Pass a function that returns the value of the ko.computed

查看:150
本文介绍了传递一个返回ko.computed值的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个函数 generate test 。当用户单击按钮时, generate 被调用,然后调用 test 函数。我收到以下错误:

I have two functions generate and test. When the user clicks a button generate is called which then calls test function. I receive the following error:


传递一个返回ko.computed值的函数。

Pass a function that returns the value of the ko.computed.

在测试函数的这一行抛出错误:

The error is thrown on this line in the test function.:

var unmapped = ko.mapping.toJS(this); 

是我的带有ko.observable的viewmodel属性。

this is my viewmodel with ko.observable properties.

以下是两个函数。如果我将 test 函数的内容移动到 generate 一切正常但我需要逻辑在两个不同的函数中。我该怎么办?

Here are the two functions. If I move the content of test function to generate everything works fine but I need the logic to be in two different functions. What can I do?

我非常困在这里。任何帮助都会大大降低。

I am very stuck here. Any help will be greatly apreciated.

generate = function () {
            if (!omega.validatableFranchiseGeneration.validate()) {
                return false;
            }
            omega.franchiseInfo.IsForGeneration(true);

            test();
        }

 var test = function () {
        getIpsAndPorts();

        for (var i = 0; i < omega.franchiseInfo.LanguagesInfoViewModel().length; i++) {
            if ($.isArray(omega.franchiseInfo.LanguagesInfoViewModel()[i].SubMenuItemsViewModel)) {
                omega.franchiseInfo.LanguagesInfoViewModel()[i].SubMenuItemsViewModel = omega.franchiseInfo.LanguagesInfoViewModel()[i].SubMenuItemsViewModel[0];
            }
        }

        var unmapped = ko.mapping.toJS(this);

        var jsonData = ko.toJSON(unmapped);
        $.ajax({
            url: "/franchise/Save",
            type: "POST",
            // data: ko.toJSON({ folderName: FolderName }),
            data: { franchiseInfoViewModel: jsonData },
            //traditional: true,
            //contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function (data, textStatus, xhr) {
                window.location.href = data.redirectToUrl;
            },
            error: function (request, status, error) {
                jsonValue = jQuery.parseJSON(request.responseText);
                omega.franchiseInfo.errorMessages([]);
                for (var i = 0; i < jsonValue.errorMessages.length; i++) {
                    omega.franchiseInfo.errorMessages.push({ errorMessage: ko.observable(jsonValue.errorMessages[i].ErrorMessage) });
                }
                for (var i = 0; i < omega.franchiseInfo.LanguagesInfoViewModel().length; i++) {
                    InitializeViewLanguagesInfo(omega.franchiseInfo.LanguagesInfoViewModel()[i]);
                }
            }
        });
    }   


推荐答案

没有看到你的所有代码很难确定,但如果代码(原样)直接包含在generate中,那么我愿意打赌这个不是你认为的那样在 test 函数中。 测试函数中的含义与 generate

Without seeing all your code this is hard to be sure, but if the code (as is) works when directly included in generate, then I'd be willing to bet that this is not what you think it is in the test function. this has a different meaning inside the test function than it did inside generate.

启动Chrome或Firebug并在 var unmapped = ko.mapping.toJS(this)上设置断点; 行。运行程序,当断点点击进入控制台并查看这个。它是你的ViewModel?

Fire up Chrome or Firebug and set a breakpoint on the var unmapped = ko.mapping.toJS(this); line. Run your program and when the breakpoint hits go to the Console and look at this. Is it your ViewModel?

如果这个是你期望的 generate 您可以随时拨打测试,如下所示:

If this is what you expect inside generate you could always call test like this:

test.apply(this);

这将明确设置的上下文对于方法调用。

This will explicitly set the context of this for the method invocation.

另一种选择是在ViewModel中设置 self 变量。这通常在顶部完成,看起来像: var self = this; 。通过创建此变量,您可以在外部函数范围内的任何函数内引用 self',而不必担心这个值的波动。

Another option is to set a self variable inside your ViewModel. This is usually done at the top and looks like: var self = this;. By creating this variable you can then reference self' inside any functions within the outer "function" scope, and not have to worry about the value ofthis` fluctuating.

-

这是一个模拟我认为发生的事情的简单小提琴: http://jsfiddle.net/jearles/aLFWe/

Here is a simple fiddle to simulate what I think is happening: http://jsfiddle.net/jearles/aLFWe/

打开Chrome或Firebug控制台并查看记录的对象。请注意,当我刚刚调用 updatea()时, Window 是记录的对象,但是对象在原始点击功能中,当我调用 updateb.apply(this) updatec()(引用 self )。

Open Chrome or Firebug console and look at the logged objects. Note that Window was the logged object when I just called updatea(), but was Object in the original click function and when I called updateb.apply(this) or updatec() (which referenced self).

这篇关于传递一个返回ko.computed值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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