[Opa] Running JavaScript onready (on application load)

Johannes Erber Hannes_E at gmx.de
Mon Feb 20 21:27:30 UTC 2012


Hi,

I want to invoke some JavaScript code without user interaction right 
after the start of my OPA. In specific, I want to show a modal window 
from twitters bootstrap UI 
(http://twitter.github.com/bootstrap/javascript.html#modals) when the 
application is ready, which can be done with the following (JavaScript) 
code snippet:

 > $('#myModal').modal('show')

After researching a bit and reading this blog post from Adam 
http://blog.opalang.org/2012/01/reusing-javascript-libraries-jquery-ui.html 
it seems the only way to run JavaScript Code from Opa is to create such 
a plugin, is that right?

So now I created a plugins/modal.js file with the following content:

 > ##extern-type string
 >
 > ##register show_modal: string -> void
 > ##args(x)
 > {
 >   $('#myModal').modal('show')
 > }

then an according plugins/modal.opa file, which looks as follows:

 > package plugins.modal
 >
 > module Modal {
 >
 >   function show_modal(x) {
 >     %%Modal.show_modal%%(x)
 >   }
 > }

altered my Makefile:

 > OPA ?= opa
 > EXE = opa_chat.exe
 >
 > all: $(EXE)
 >
 > plugins: plugins/modal.js
 >   opa-plugin-builder --js-validator-off plugins/modal.js -o modal.opp
 >
 > $(EXE): plugins src/*.opa resources/*
 >   $(OPA) plugins/modal.opa modal.opp
 >   $(OPA) src/*.opa -o $(EXE)

and finally I can invoke the function call from inside a function of my 
OPA code which is run at the start of my application (invoked by a 
onready tag), via

 > Modal.show_modal("")

This works fine, the modal window shows up on startup (which makes me 
very happy) but I am still wondering, if this is the right way of doing 
this or if there is an much simpler way of executing JavaScript code (on 
startup), which I might not have seen yet (all I have to do is actually 
just evaluate the JQuery expression as shown above).

You may also have noticed that I am using an argument for the JS 
function which is actually redundant. But creating the example above 
without an argument throws the error

 > Function was found of type {  } but application expects it to be of
 > type -> 'a.

Could such a function also be created without arguments?

Cheers
Johannes


More information about the Opa mailing list