I want to take a JS library designed and packaged for Node.js and use it with QML (specifically the ProtoBuf library). The ProtoBuf library relies on the ByteBuffer library.
I edited bytebuffer.js and protobuf.js to add .pragma library as the first line. Then, in my .qml I have:
import "qrc:/scripts/bytebuffer.js" as BB
import "qrc:/scripts/protobuf.js" as PB
Window {
Component.onCompleted: {
console.log('PB', PB);
console.log('func',PB.loadProtoFile);
for (var k in PB) console.log(k,typeof PB[k],PB[k]);
console.log('done!')
}
}
The output I see is:
qml: PB [object Object]
qml: func undefined
qml: done!
So, QML is loading the ProtoBuf library in some sense (and I've put debug messages in that library and confirmed that the generic loader is running and able to find ByteBuffer), but it's not an object with the methods that I expect, like loadProtoFile().
How do I get to the ~real ProtoBuf object that the library exposes? Or how should I modify the libraries to work with QML properly?
I understand now that the way QML namespaces libraries, the statement
import "qrc:/scripts/protobuf.js" as PB
causes PB to be the global scope in which the library script was run. As such, the 'real' ProtoBuf that the library exposes in this code:
(global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);
can be found in QML as:
var ProtoBuf = PB.dcodeIO.ProtoBuf;
console.log( ProtoBuf.loadProtoFile );
//-> function() { [code] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With