2009-06-11 24 views
10

Ho un metodo JavaScript nativo in una delle mie classi Java GWT, ma sto avendo difficoltà chiamare i miei metodi Java dal codice nativo Javascript. Ho provato a seguire this il più vicino possibile, ma non riesco a farlo funzionare. L'ho compilato e eseguito in Firefox, e la console di errore ha detto "Errore: this.lc non è una funzione". Ho provato a cambiare tutti i metodi per pubblico, ma quello non sembrava fare la differenza. Che cosa sto facendo di sbagliato?metodo nativo javascript nel GWT

package com.proprintsgear.design_lab.client; 
... 
public class ValueBox extends HorizontalPanel { 
... 
private void fireChange() { 
    ... 
} 

private void increaseValue() { 
    ... 
} 

private native void addNativeMouseWheelListener(String id) /*-{ 
    function mouseOverHandler(e) { 
     $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false); 
    } 

    function mouseOutHandler(e) { 
     $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false); 
    } 

    function scrollWheelMove(e) { 
     if ($wnd.event || $wnd.Event) { 
      if (!e) e = $wnd.event; 
      if (e.wheelDelta <= 0 || e.detail > 0) { 
       $wnd.alert("DOWN"); 
      } else { 
       [email protected]_lab.client.ValueBox::increaseValue()(); 
      } 
      [email protected]_lab.client.ValueBox::fireChange()(); 
     } 
    } 

    var box=$doc.getElementById(id); 
    box.addEventListener("mouseout",mouseOutHandler,false); 
    box.addEventListener("mouseover",mouseOverHandler,false); 
}-*/; 

risposta

11

In tutto il codice che ho fatto in passato, non ho mai usato 'questo' per identificare la mia classe, ho passato la classe in

esempio:. Cambiare questa:

private native void addNativeMouseWheelListener(String id) /*-{ 
    function mouseOverHandler(e) { 
     $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false); 
    } 

    function mouseOutHandler(e) { 
     $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false); 
    } 

    function scrollWheelMove(e) { 
     if ($wnd.event || $wnd.Event) { 
       if (!e) e = $wnd.event; 
       if (e.wheelDelta <= 0 || e.detail > 0) { 
         $wnd.alert("DOWN"); 
       } else { 
         [email protected]_lab.client.ValueBox::increaseValue()(); 
       } 
       [email protected]_lab.client.ValueBox::fireChange()(); 
     } 
    } 

    var box=$doc.getElementById(id); 
    box.addEventListener("mouseout",mouseOutHandler,false); 
    box.addEventListener("mouseover",mouseOverHandler,false); 
}-*/; 

a tal:

private native void addNativeMouseWheelListener(ValueBox instance, String id) /*-{ 
    function mouseOverHandler(e) { 
     $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false); 
    } 

    function mouseOutHandler(e) { 
     $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false); 
    } 

    function scrollWheelMove(e) { 
     if ($wnd.event || $wnd.Event) { 
       if (!e) e = $wnd.event; 
       if (e.wheelDelta <= 0 || e.detail > 0) { 
         $wnd.alert("DOWN"); 
       } else { 
         [email protected]_lab.client.ValueBox::increaseValue()(); 
       } 
       [email protected]_lab.client.ValueBox::fireChange()(); 
     } 
    } 

    var box=$doc.getElementById(id); 
    box.addEventListener("mouseout",mouseOutHandler,false); 
    box.addEventListener("mouseover",mouseOverHandler,false); 
}-*/; 
+2

Perfetto! Questo ha senso, ma vorrei che fosse documentato meglio sul sito GWT. – DLH

4

ho trovato un modo migliore. È simile a ciò che fai in JavaScript, dove imposti "var that = this". Usando questo approccio, non c'è bisogno di passare questo a listenForPostMessage():

protected native void postMessage(String msg) /*-{ 
    $wnd.postMessage(msg, "*"); 
}-*/; 

private final native void listenForPostMessage() /*-{ 
    var that = this; 
    $wnd.addEventListener("message", function(msg) { 
    [email protected]::onPostMessage(Ljava/lang/String;Ljava/lang/String;)(
    msg.data, msg.origin); 
    }); 
}-*/; 

private void onPostMessage(String data, String origin) { 
    Label msgLabel = new Label(); 
    msgLabel.setText("GWT received a postMessage: Data: " + 
     data + " Origin: " + origin); 
    mainPanel.add(msgLabel); 
} 
Problemi correlati