Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Contributor
Posts: 5
Registered: ‎03-30-2010
My Device: BlackBerry 9550
My Carrier: Telecom Italia
Accepted Solution

Widget Javascript Extension: Callback

Hi guys,

I'm trying to develop some widgets using also Javascript Extension.

 

I'm facing a problem.

In my class extending ScriptableFunction, how I can manage an object passed to invoke methods as arguments and that represents a "pointer" to javascript function to be used as asyncronous callback?

 

How I can invoke it?

 

Thank you very much.

 

Regards

Scarex

New Contributor
Posts: 5
Registered: ‎03-30-2010
My Device: BlackBerry 9550
My Carrier: Telecom Italia

Re: Widget Javascript Extension: Callback

I discovered that a javascript function passed via invoke method is recognized as net.rim.ecmascript.engine.HostFunctionWrapper (by using arg[i].getClass method)

 

But I'm not able to find this class anywhere...

Retired
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Widget Javascript Extension: Callback

Hi Scarex,

 

That's a really great question.  You can execute a JavaScript statement from Java using the executedScript() method which accepts a string as its parameter.  Instead of passing in a pointer to a JavaScript method, you can provide the name of the function along with any parameters.

 

Here's an example of how you could pass two Integer arguments to a ScriptableFunction, and make a call back to a JavaScript function named "add", passing these values:

 

 

ScriptableFunction _myFunc = new ScriptableFunction() 
{
   public Object invoke(Object obj, Object[] args) throws Exception
   {
      if (args.length == 2) {
         int param1 = ((Integer)args[0]).intValue();
         int param2 = ((Integer)args[1]).intValue();
         _browserField.getScriptEngine().executeScript("add(" + param1 + ", " + param2 + ")", null);
      }
      return UNDEFINED;
   }
};  

Here is what the add() JavaScript method, defined within the web content of the BlackBerry Widget application, could look like:

 

 

<script type="text/JavaScript">
   function add(param1, param2)
   {
      alert(param1 + param2);
   }
</script>

 

 

 

I hope that helps.

 

Best wishes,

Adam

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
New Contributor
Posts: 5
Registered: ‎03-30-2010
My Device: BlackBerry 9550
My Carrier: Telecom Italia

Re: Widget Javascript Extension: Callback

Definitely! It's cool Smiley Happy

 

Once defined the correct order of execution, I can also use some javascript wrapper to solve problems..

 

Thank you very much!

Contributor
Posts: 35
Registered: ‎10-06-2010
My Device: Bold 9650
My Carrier: Verizon

Re: Widget Javascript Extension: Callback

The HostFunctionWrapper object is a ScriptableFunction. After casting to a ScriptableFunction, just call ScriptableFunction.invoke( obj, args )

Joe Davis
Developer | Metova
http://www.metova.com
Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Widget Javascript Extension: Callback

 


j_davis_metova wrote:

The HostFunctionWrapper object is a ScriptableFunction. After casting to a ScriptableFunction, just call ScriptableFunction.invoke( obj, args )


 

Exactly,

 

You can pass in the actual call back function as a parameter, then cast it as a ScriptableFunction and execute it using the invoke() method and pass whatever arguments that you like.

 

This way you can actually hold a pointer to the function in your extension and execute it whenever you like Smiley Happy

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Highlighted
New Developer
Posts: 21
Registered: ‎07-13-2009
My Device: Not Specified

Re: Widget Javascript Extension: Callback

I'm having a bit a tricky time with some interesting behaviour with javascript extensions.

 

In the native code a thread is started to perform some work and then a callback function is called (originally passed in as an argument). However the javascript function loses all references to other variables:

 

i.e.

 

function uiCallback(){

alert("Hello");

};

nativecall.call(function(){

alert("Initial hello");

uiCallback();

});

 

What will happen is the "initial hello" message will appear but the second "hello" message will not because uiCallback is null. If I remove the native code from doing it's work in a thread it works. I imagine this is some issue of calling a pointer from another thread but because of the mash of java and javascript its difficult to appreciate at what stage the reference is being lost and hence attempt to work out what is causing the fault.

 

Does anyone have any experience or knowledge about this problem?

 

Cheers,

Matt

 

 

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Widget Javascript Extension: Callback

Hi Matt,

 

I just tried out that scenario with my Spinner extension that I am making thread safe and it doesn't seem to have a problem with it.  I've first tested this on OS 5.0 and will be moving on to test on BlackBerry 6.

 

I haven't uploaded the code yet, but I have Forked the main github project.  If you want you can watch my fork so that you can see the code when I upload it

 

https://github.com/tneil/WebWorks

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter