2010-11-17 18 views
5

tôi đang xuất một phương pháp GWT để javascript mẹ đẻ theo cách sau đây:Tìm hiểu khi một module GWT đã nạp

public class FaceBookGalleryEntryPoint implements EntryPoint { 

    @Override 
    public void onModuleLoad() { 

     FacebookGallery facebookGallery = new FacebookGallery(); 
     RootPanel.get().add(facebookGallery); 

     initLoadGallery(facebookGallery); 
    } 

    private native void initLoadGallery(FacebookGallery pl) /*-{ 
     $wnd.loadGallery = function (galleryId) { 
      [email protected]::loadGallery(Ljava/lang/String;)(galleryId); 
     }; 
    }-*/; 
} 

Trong trang chủ, tôi đang cố gắng để gọi nó:

<html> 
    <head> 
     <title>Facebook image gallery</title> 
     <script type="text/javascript" 
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>  
    </head> 

    <body> 
     <script type="text/javascript" src="/fbg/fbg.nocache.js"></script> 
     <h1>Facebook gallery test</h1> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       loadGallery('blargh');    
      }); 
     </script> 
    </body> 
</html> 

Thật không may, khi gọi lại document.ready được gọi, hàm chưa được xác định. Khi được thực hiện thủ công từ giao diện điều khiển Firebug, hàm hoạt động tốt.

Tôi có thể thực hiện một số cuộc thăm dò sau mỗi 50 mili giây cho đến khi tôi tìm thấy một hàm được xác định bằng tên đó, nhưng nó có vẻ giống như một cách tiếp cận khủng khiếp.

Làm cách nào để nhận thông báo khi mô-đun được tải và do đó khi chức năng có sẵn?

Trả lời

12

Tôi sẽ cố định nghĩa hàm gọi lại trong trang chủ và gọi nó từ GWT ở cuối phương thức onModuleLoad().

Hostpage chức năng:

<script type="text/javascript"> 
    function onGwtReady() { 
    loadGallery('blargh');    
    }; 
</script> 

GWT:

public void onModuleLoad() { 
    FacebookGallery facebookGallery = new FacebookGallery(); 
    RootPanel.get().add(facebookGallery); 

    initLoadGallery(facebookGallery); 

    // Using a deferred command ensures that notifyHostpage() is called after 
    // GWT initialisation is finished. 
    DeferredCommand.addCommand(new Command() { 
    public void execute() { 
     notifyHostpage(); 
    } 
} 

private native void notifyHostpage() /*-{ 
    $wnd.onGwtReady(); 
}-*/ 
+0

Cảm ơn, trông thú vị - đặc biệt là các bit DeferredCommand. Tôi sẽ chụp ảnh này tối nay. –

+2

BTW, với GWT 2.1 bạn nên sử dụng Scheduler(). Get(). ScheduleDeferred() –

+0

Cảm ơn gợi ý Trình lập lịch biểu. – vanje

Các vấn đề liên quan