2011-08-17 30 views
5

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(); 
    } 

} 

Trả lời

0

Tôi nghĩ vấn đề là bạn không xác định bất cứ điều gì trong đối tượng cửa sổ của bạn, QtObject chỉ chứa windowObjectName nhưng không có chức năng hoặc vars. windowObjectName thực sự chỉ là tên cho đối tượng mới, qml không sử dụng "fb.js" -import cho đối tượng đó.

Theo docs nó phải giống như thế này:

WebView { 
    javaScriptWindowObjects: QtObject { 
    WebView.windowObjectName: "FB" 

    // the stuff you want in that window-object goes here: 
    function init() { 
     console.log("FB.init"); 
    } 
    } 
} 
Các vấn đề liên quan