0

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

Avi A
  • 69
  • 8
  • possible duplicate of [Uncaught TypeError: undefined is not a function on loading jquery-min.js](http://stackoverflow.com/questions/10429838/uncaught-typeerror-undefined-is-not-a-function-on-loading-jquery-min-js) – claj May 05 '14 at 10:42

2 Answers2

0

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();
}-*/;
Braj
  • 46,415
  • 5
  • 60
  • 76
  • this answer is the correct one, But i just wan't to know if it's normal that the alert is showed after 7 seconds from the initial time that the page has been loaded? – Avi A May 05 '14 at 13:06
  • yes `nocache.js` takes time to load and to export the method to `JavaScript`. `body onload()` doesn't mean that `EntryPoint#onModuleLoad()` has been called. – Braj May 05 '14 at 13:10
  • It depends on the size of `nocache.js` also. There will no issue if you call it after exported it to `JavaScript`. – Braj May 05 '14 at 13:14
0

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));
    }
}-*/;
Vadim
  • 1,125
  • 8
  • 18