I have an appjs application running well. I am trying to basically either call an existing javascript function from within the loaded page on an event. OR access the pages elements on an event. I am using the node-serial library with a Honeywell Barcode Scanner. 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 i.e.
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. When I run these things in the
window.on('ready', function(){ ... });
I'm able to access the elements on the page just fine. i.e.
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
$('#message').html('Please wait...');
^
TypeError: object is not a function...
Or even $("#message").html that "html" is not a function. But if i run it on the window.on ready function it works. I'm pretty lost as there is very little documentation for appjs. Thank you!
The 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:
window.$('#message').html('Please wait...');
To be able to call a custom function (called scan) do the following in 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:
<body>
<button onclick="window.scan();">Scan Now!</button>
</body>
Have you tried accessing it from the top of your JS files? It seems like Window is undefined rather than the $ instance.
I did this in a recent project to access DOM nodes using jQuery in a node module I'd written. Add this to your app.js file, it's using the Node global variable
// Under your window definition
global.$ = window.$;
That would allow you to use $ without the window prefix in all of your node files/modules.