java - How can functions in a Griffon applet be called from JavaScript? -


we have project web-based, needs have significant access user's filesystem. html doesn't (yet) allow enough access, , since grails shop, having file access code applet makes far more sense having flash code.

we've dabbled little bit griffon (a previous prototype project 100% griffon webstart app), , love structure , "boilerplate removal" griffon gives applets. issue keep running into, however, connecting html / javascript ui griffon applet of work.

we found james williams' excellent example of calling javascript functions griffon app, haven't been able find example of reverse: calling griffon functions javascript side.

the primary issue want have griffon app "headless" (or close can get), since of ui handled javascript , html. means can't use ui events triggered within griffon app, way james in example.

does have example of javascript interacting griffon applet learn from? or advice on how surface things within griffon applet javascript?

sounds me want call java (or groovy!) function found within applet outside world using javascript, right?

in order work:

  1. you must have livescript enabled , the applet's jar must signed.

  2. the next step define entry point in applet subclass knows how handle call want make.

    this gets tricky because default applet class griffon.swing.swingapplet must create own subclass of griffon.swing.swingapplet , use main entry point.

    your subclass 1 defines method (or methods) callable javascript side. should along lines of:

    import griffon.swing.swingapplet;    public class myswingapplet extends swingapplet {     // match superclass constructors     public calculatorapplet(string[] args) {           super(args);       }        public object myappletmethod(string[] args) {         // args come form js world         // whatever necessary here     } } 
  3. to tell griffon use our custom applet subclass instead of default, create file named griffon-app/scripts/_events.groovy , place following inside:

    eventpackageappstart = {       griffonappletclass = 'calculator.calculatorapplet'   } 
  4. finally, grab hold of applet object javascript , invoke myappletmethod() values see fit:

    <script>       function talktoapplet() {         var r = document.applets.myappletid.myappletmethod("arg1", "arg2");           alert(r);     } </script> 

a more detailed example can found on my blog.


Comments