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

1

2 Answers 2

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();
}-*/;
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
It depends on the size of nocache.js also. There will no issue if you call it after exported it to JavaScript.
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));
    }
}-*/;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.