I have a QML file that imports a JavaScript library:
import "qrc:/scripts/protobuf.js" as PB
This library modifies the 'global' object during setup. Simplified, the JS library is:
.pragma library
(function(global){
global.dcodeIO = global.dcodeIO || {};
global.dcodeIO.ProtoBuf = {}; // In reality, a complex object
})(this);
On Windows and Linux this works as expected; later in my QML file I write var ProtoBuf = PB.dcodeIO.ProtoBuf; and it finds the dcodeIO property added to the 'global' object and properly gives me the object I need.
However, on another platform, the same code does not work. I get an error that it cannot read property ProtoBuf of undefined. I add debugging lines to my QML and see:
console.log(PB.dcodeIO); //-> undefined
for (var k in PB) console.log(k,PB[k]); //-> (no enumerable properties logged)
Yet, the JavaScript code in the library is loaded and runs. Within the library if I add console.log(global.dcodeIO) after the line linked above I see [object Object].
What might the difference be? How can I determine why Qt is running my JavaScript file, but not successfully associating the global object with PB?