简体   繁体   English

使用Chrome控制台通过RequireJS访问Knockout ViewModel

[英]Using Chrome Console to Access Knockout ViewModel with RequireJS

How do I access the KnockOut ViewModel variables in the Chrome console now that I am using RequireJS? 现在我正在使用RequireJS,如何访问Chrome控制台中的KnockOut ViewModel变量?

Before using RequireJS, I followed a namespacing pattern, hiding everything within a single global. 在使用RequireJS之前,我遵循了命名空间模式,将所有内容隐藏在一个全局内。 I could access the global by typing the following into the Chrome console: window.namespaceVar. 我可以通过在Chrome控制台中键入以下内容来访问全局:window.namespaceVar。

But now that I am using RequireJS, all my variables are hidden behind the require function. 但是现在我正在使用RequireJS,我的所有变量都隐藏在require函数后面。

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

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

    ko.applyBindings(new ViewModel());
}

So how would I access the current value of testVar in the example? 那么如何在示例中访问testVar的当前值?

Knockout includes the functions ko.dataFor and ko.contextFor that will give you access to the KO view model information given an element. Knockout包含函数ko.dataForko.contextForko.dataFor ko.contextFor您访问给定元素的KO视图模型信息。

So, in the console, you can do something like: 因此,在控制台中,您可以执行以下操作:

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

In your case, testVar is not exposed, so you would still not be able to access it. 在您的情况下, testVar未公开,因此您仍然无法访问它。 I assume that yours was just a sample though and you meant something like: 我认为你的只是一个样本,你的意思是:

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

Now, using the above method you would be able to access vm.testVar and its value by doing vm.testVar() 现在,使用上面的方法,您可以通过执行vm.testVar()来访问vm.testVar及其值

Here are the docs that we have on these functions: http://knockoutjs.com/documentation/unobtrusive-event-handling.html 以下是我们对这些功能的文档: http//knockoutjs.com/documentation/unobtrusive-event-handling.html

and here's a step-by-guide on how to debug KnockoutJS with chrome: http://devillers.nl/quick-debugging-knockoutjs-in-chrome/ 以下是如何使用chrome调试KnockoutJS的逐步指南: http//devillers.nl/quick-debugging-knockoutjs-in-chrome/

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

As Ryan suggested, the quickest way is to use ko.contextFor and ko.dataFor in the console to see the binding context of an element on the dom. 正如Ryan所说,最快的方法是在控制台中使用ko.contextForko.dataFor来查看dom上元素的绑定上下文。

There's also a very useful Chrome Extension that uses this principle called KnockoutJS Context Debugger, available here: 还有一个非常有用的Chrome扩展程序使用了这个名为KnockoutJS Context Debugger的原理,可在此处获得:

Chrome Web Store - KnockoutJS Context Debugger Chrome网上应用店 - KnockoutJS上下文调试器

It allows you to inspect an element and see it's context in the sidebar of the elements pane. 它允许您检查元素并在元素窗格的侧栏中查看它的上下文。 It's most useful it you have multiple binding contexts on a page, or very nested binding contexts. 最有用的是你在页面上有多个绑定上下文,或者非常嵌套的绑定上下文。

Require is all about not having globals: 要求就是没有全局变量:

require(["knockout"],function(ko){ window.ko=ko;}); 

is introducing globals again 正在再次引入全局变量

You can use this in the console: 您可以在控制台中使用它:

require("knockout").dataFor($0);
require("knockout").contextFor($0);

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

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