使用Chrome控制台通过RequireJS访问Knockout ViewModel [英] Using Chrome Console to Access Knockout ViewModel with RequireJS

查看:84
本文介绍了使用Chrome控制台通过RequireJS访问Knockout ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在使用RequireJS,如何访问Chrome控制台中的KnockOut ViewModel变量?

How do I access the KnockOut ViewModel variables in the Chrome console now that I am using RequireJS?

在使用RequireJS之前,我遵循了命名空间模式,隐藏了所有内容一个全球性的。我可以通过在Chrome控制台中键入以下内容来访问全局:window.namespaceVar。

Before using RequireJS, I followed a namespacing pattern, hiding everything within a single global. I could access the global by typing the following into the Chrome console: window.namespaceVar.

但是现在我使用的是RequireJS,我的所有变量都隐藏在require函数后面。

But now that I am using RequireJS, all my variables are hidden behind the require function.

require(['knockout-2.2.0', 'jquery'], function (ko, jQuery) {

    var ViewModel = function () {
            var testVar = ko.observable(true);
        };

    ko.applyBindings(new ViewModel());
}

那么如何获取的当前值?示例中的testVar

推荐答案

Knockout包含函数 ko.dataFor ko.contextFor ,可让您访问给定元素的KO视图模型信息。

Knockout includes the functions ko.dataFor and ko.contextFor that will give you access to the KO view model information given an element.

因此,在控制台中,您可以执行以下操作:

So, in the console, you can do something like:

var vm = ko.dataFor(document.body);

在您的情况下, testVar 没有暴露,所以你仍然无法访问它。我假设你的只是一个样本,你的意思是:

In your case, testVar is not exposed, so you would still not be able to access it. I assume that yours was just a sample though and you meant something like:

var ViewModel = function () {
     this.testVar = ko.observable(true);
};

现在,使用上述方法,您将能够访问 vm.testVar 及其值 vm.testVar()

Now, using the above method you would be able to access vm.testVar and its value by doing vm.testVar()

以下是文档我们有以下功能: http://knockoutjs.com/documentation/unobtrusive-event-handling .html

Here are the docs that we have on these functions: http://knockoutjs.com/documentation/unobtrusive-event-handling.html

这里是关于如何使用chrome调试KnockoutJS的逐步指南:
http://devillers.nl/quick-debugging-knockoutjs-in-chrome/

and here's a step-by-guide on how to debug KnockoutJS with chrome: http://devillers.nl/quick-debugging-knockoutjs-in-chrome/

使用Chrome的$ 0_ $ 4功能: https://developers.google.com/chrome-developer-tools/docs/commandline-api#0-4

using Chrome's $0_$4 feature: https://developers.google.com/chrome-developer-tools/docs/commandline-api#0-4

这篇关于使用Chrome控制台通过RequireJS访问Knockout ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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