简体   繁体   English

使用nodejs + appjs + node-serial访问dom

[英]accessing dom with nodejs + appjs + node-serial

I have an appjs application running well. 我有一个运行良好的appjs应用程序。 I am trying to basically either call an existing javascript function from within the loaded page on an event. 我正在尝试基本上从事件的已加载页面内调用现有的javascript函数。 OR access the pages elements on an event. 或访问事件中的页面元素。 I am using the node-serial library with a Honeywell Barcode Scanner. 我将节点序列库与Honeywell条形码扫描仪一起使用。 I can access the serial port fine, I can get the scan data just fine with: 我可以正常访问串行端口,可以通过以下方式获得扫描数据:

serialPort.on('data', function(data) { ... });

What I want to be able to do is, as stated above, call a javascript function on the page ie 我想要做的是,如上所述,在页面上调用javascript函数,即

window.document.body.scan(data);

OR at least access the data and be able to call / make http requests with node, which i'm able to do fine as well. 或至少访问数据并能够用node调用/发出http请求,我也能做到这一点。 When I run these things in the 当我在

window.on('ready', function(){ ... });

I'm able to access the elements on the page just fine. 我可以正常访问页面上的元素。 ie

var $ = window.$;
$("#message").html('Something...');

But if i try to run the exact same code in the serialPort.on data function i get errors like 但是如果我尝试在serialPort.on数据函数中运行完全相同的代码,则会收到类似以下错误

        $('#message').html('Please wait...');
        ^
TypeError: object is not a function...

Or even $("#message").html that "html" is not a function. 甚至$(“#message”)。html都说“ html”不是函数。 But if i run it on the window.on ready function it works. 但是如果我在window.on就绪功能上运行它,它将起作用。 I'm pretty lost as there is very little documentation for appjs. 我非常迷茫,因为关于appjs的文档很少。 Thank you! 谢谢!

The serialPort.on('data',function(){ ... }); serialPort.on('data',function(){...}); code is being run in AppJS so to access the window you need to use the Window variable, try changing your code to: 代码正在AppJS中运行,因此要访问窗口,您需要使用Window变量,请尝试将代码更改为:

window.$('#message').html('Please wait...');

To be able to call a custom function (called scan) do the following in window.on('ready', function(){ ... }); 为了能够调用自定义函数(称为扫描),请在window.on('ready',function(){...});中进行以下操作。

window.scan = function() {
    //code to connect to the scanner here.
    console.log("scan run");
}

Then somewhere on the html page inside something like a button you can then call that function: 然后在html页面上某个按钮之内的某个位置,然后可以调用该函数:

 <body>
 <button onclick="window.scan();">Scan Now!</button>
 </body>

Have you tried accessing it from the top of your JS files? 您是否尝试过从JS文件的顶部访问它? It seems like Window is undefined rather than the $ instance. 似乎Window是未定义的,而不是$实例。

I did this in a recent project to access DOM nodes using jQuery in a node module I'd written. 我在最近的项目中做到了这一点,以便在我编写的节点模块中使用jQuery访问DOM节点。 Add this to your app.js file, it's using the Node global variable 使用Node全局变量将其添加到您的app.js文件中

// Under your window definition
global.$ = window.$;

That would allow you to use $ without the window prefix in all of your node files/modules. 这将允许您在所有节点文件/模块中使用$而没有窗口前缀。

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

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