Tôi đang cố gắng để QML tương tác với API biểu đồ FB bằng cách sử dụng FB Javascript SDK.Có cách nào QML có thể tải dữ liệu bằng Facebook Javascript SDK không?
Tôi tải HTML này bên trong một yếu tố WebView:
html: "<script>console.log(\"This is in WebKit!\"); window.FB.init();</script>"
và tôi cũng đã tạo ra một đối tượng Window JS tên FB bên trong WebView:
javaScriptWindowObjects: QtObject {
WebView.windowObjectName: "FB"
}
Nhưng ngay sau khi window.FB.init() được gọi, nó phát ra một lỗi:
ReferenceError: Can't find variable: window
Cách tiếp cận khác mà tôi đang sử dụng là nạp FB .init() chức năng sử dụng Component.onComplete
function startupFunction() {
console.log("This call is in QML!");
FB.init({
appId:'XXXXXXXXXXXXX', cookie:true,
status:true
});
console.log(FB);
}
Component.onCompleted: startupFunction();
Nhưng tôi nhận được lỗi như:
TypeError: Result of expression 'FB.init' [undefined] is not a function
Dưới đây là hoàn tất các QML:
import QtQuick 1.0
import "fb.js" as FB
import QtWebKit 1.0
Rectangle {
width: 360
height: 360
Text {
text: "Hello World"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
}
WebView {
preferredWidth: 490
preferredHeight: 400
scale: 0.5
smooth: false
javaScriptWindowObjects: QtObject {
WebView.windowObjectName: "FB"
}
html: "<script>console.log(\"This is in WebKit!\"); window.FB.init();</script>"
function startupFunction() {
console.log("This call is in QML!");
FB.init({
appId:'xxxxxxxxxxxx', cookie:true,
status:true
});
console.log(FB);
}
Component.onCompleted: startupFunction();
}
}