Question

I have a piece of code i haven been written, And i don't understand why its not working

I have written:

public class MyUtility {
    public static int computeLoanInterest(int amt, float interestRate, int term) {
       return (amt*term);   
    }
    public static native void exportStaticMethod() /*-{
        $wnd.computeLoanInterest =
        $entry(@com.myapp.appname.client.MyUtility::computeLoanInterest(IFI));
    }-*/;
}

on my client java entrypoint:

       public void onModuleLoad() {
           MyUtility.exportStaticMethod();
       }

and on my handwritten javascript code:

    <head>
    <script type="text/javascript" language="javascript" src="projv1/projv1.nocache.js"></script>
    <script type="text/javascript">
        function mainl(){
          var it=window.computeLoanInterest(5,2,2);
          alert(it);
        }
    </script>
    </head>

    <body onload="mainl()">
    </body>

but i'm getting an error on the console of the browser:

Uncaught TypeError: undefined is not a function

Was it helpful?

Solution

Call the function after exporting to JavaScript via JSNI that is more precise and accurate.

public static native void exportStaticMethod() /*-{
    $wnd.computeLoanInterest = $entry(@com.myapp.appname.client.MyUtility::computeLoanInterest(IFI));

    $wnd.mainl();
}-*/;

OTHER TIPS

This should work:

public static native void exportStaticMethod() /*-{
    $wnd.computeLoanInterest = function(amt, interestRate, term) {
        return ($entry(@com.myapp.appname.client.MyUtility::computeLoanInterest(IFI))(amt, interestRate, term));
    }
}-*/;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top