External JavaScript and async callbacks

187 views
Skip to first unread message

Danjel

unread,
Apr 20, 2007, 10:01:14 AM4/20/07
to Google Web Toolkit
Hi

Im writing a application witch will have a few pages written outside
of gwt.

In my rcp i have 2 functions getSessionVar, and setSessionVar, they
will be used in gwt to store and retrieve sessioninfo.

the methods in the server simply does the following:

public String getSessionVar(String key) throws GWTException {
return "" + (String)
getThreadLocalRequest().getSession().getAttribute(key);
}

public void setSessionVar(String key, String value) throws
GWTException {
if (key.equalsIgnoreCase("session")) {
throw new GWTException();
}
getThreadLocalRequest().getSession().setAttribute(key, value);
}

and the client functions access them this way:

public static void getSessionVar(String key, final Command onSuccess)
{

final IEWBAsync svc = getSvc();
final String session = getSession();

AsyncCallback callback = new AsyncCallback() {
public void onSuccess (Object result){
onSuccess.execute();
}
public void onFailure (Throwable ex){
Window.alert(ex.toString());
onSuccess.execute();
}
};
svc.getSessionVar(key, callback);
}

This works fine, but lets say i want to access these methods from
external javascript...

I have defined the function in $wnd, but how do i make the callback to
the external javascript when the response arrives to the onSuccess
Callback?

public static native void defineBridges([GWTMODULECLASS] module) /*-{
$wnd._getSessionVar = function(key,callback) {
module.@se.carasoft.eureka.client.CaraDoc::getSssionVar(Ljava/lang/
String;)(key);
};
}-*/;

I could call a fixed fuction in javascript like

$wnd.fixedFunction(result)

but i would like to supply a callback that should be called with the
result from the rpc call

Hope you understand what i mean.


Regards
/Danjel

Danjel

unread,
Apr 24, 2007, 7:51:35 AM4/24/07
to Google Web Toolkit
I managed to solve the problem myself. this is how i did, in case
someone else gets caught in the same problem.

the call in the external page now looks like this:

<input type="button" value="Show me"
onClick="getSessionVar('testvar', 'returnFunc');">

<script language = "javascript">
function returnFunc(txt){
alert(txt);
}
</script>


the bridge handles the window frame name and supplies it to the nativt
function.

function getSessionVar(key,func){
parent._getSessionVar(key,window.self.name,func);
}

and the instance function just ports the functionname, and the
framename to a new nativt function that uses eval

public void getSessionVar(String key,final String frame, final String
func){
final StringBuffer result = new StringBuffer("");
Command cmd = new Command(){
public void execute() {
notifyJS(result.toString(), frame, func);
}
};
ConfigHelper.getSessionVar(key, result, cmd);
}


this the final native function that gives the result back to the
specified function in the original frame using eval.

public static native void notifyJS(String res, String frame, String
func) /*-{
eval('$wnd.'+ frame + '.'+ func + '("' + res + '");');
}-*/;


regards
/Danjel


Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages