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: 9
Registered: ‎10-19-2010
My Device: 9700

Callback function in JavaScript extension [using "call by name" mechanism]

Hi,

 

I am using a callback mechanism in my BB widget application which has JS extension for server communication.

 

The callback mechanism is needed since I want to manipulate database as per the response from the server.

 

I am able to achieve this functionality through Callback mechanism in JS extension and right now I am using call by reference mechanism for callbacks as below.

 

 

String _courseParams[] = new String[1];
_courseParams[0] = data;
ScriptableFunction _dbCallBack = (ScriptableFunction) _callBackFunction;
_dbCallBack.invoke(_dbCallBack, _courseParams);

String _courseParams[] = new String[1]; _courseParams[0] = data; ScriptableFunction _dbCallBack = (ScriptableFunction) _callBackFunction; _dbCallBack.invoke(_dbCallBack, _courseParams);

 

 

It works well if am on the same page in widget, but when I navigate to the other page, then it looses the reference to the callback function to be called in the JS extension.

 

So I am wondering if there is any way to have the callback mechanism via "call by name" instead of "call by reference", so that the same function can be invoked regardless of the user navigation.

 

If someone can help me out in this situation then it would be really very helpful for me.

 

So basic problem is "I want to have the callback in JS Extension via "call by name" so that I can move away from the page which initiate request to my extension.

 

 

Thanks in advance.

 

_Ziya.

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

Re: Callback function in JavaScript extension [using "call by name" mechanism]

If you keep a handle to the BrowserField object in your extension you can call the "executeScript(string)" function that will execute whatever JavaScript you wish.  So you could call your function by doing something like 

 

 

myBrowserField.executeScript("doSomething('foo')");

 

It will then execute the script within the context of the loaded page

 

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Contributor
Posts: 21
Registered: ‎04-23-2011
My Device: 9700
My Carrier: xl

Re: Callback function in JavaScript extension [using "call by name" mechanism]

How to keep  handle  the BrowserField object in extension ?? 

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

Re: Callback function in JavaScript extension [using "call by name" mechanism]

When writing your own Java-JavaScript extension, a reference to the active browserField is passed into the register() method found within the WidgetExtension class.  You can save a reference to this object, and then pass it into the constructors of your Scriptable class.

 

See this article for more info about writing your own custom JavaScript extension:

http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/Building-a-BlackBerry-WebWorks-J...

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Contributor
Posts: 21
Registered: ‎04-23-2011
My Device: 9700
My Carrier: xl

Re: Callback function in JavaScript extension [using "call by name" mechanism]

hi... I'm verry new..

 

I try it... can you have sample code ??

How to passing it to scriptTable class

Below is my code :

 

ConnExtension.java

package connections;

import org.w3c.dom.Document;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.web.WidgetConfig;
import net.rim.device.api.web.WidgetExtension;
public final class ConnExtensions implements WidgetExtension {

	public String[] getFeatureList() {
	        String[] result = new String[1];
	        result[0] = "socket.connection";
	        return result;
	}

    public void loadFeature(String feature, String version,
			   Document doc, ScriptEngine scriptEngine) throws Exception {
			         if (feature == "socket.connection") {
			            scriptEngine.addExtension("socket.connection", new ConnNamespace());
			            
			         }
			      }

	public void register(WidgetConfig arg0, BrowserField arg1) {
		// TODO Auto-generated method stub
		

	}

	public void unloadFeatures(Document arg0) {
		// TODO Auto-generated method stub

	}

}

 ConnNameSpace.java

 

package connections;

import net.rim.device.api.script.Scriptable;

public final class ConnNamespace extends Scriptable {
	 //  public static final String socketStatus = "CONNECTED";
	   public static final String DATASOC = "data";
	   private SocketFunction doConnect;
	   public ConnNamespace() {
		      this.doConnect = new SocketFunction();
		   }
	   // The getField() function is called when the 
	   //  dot '.' extender is used on your JavaScript object.
	   public Object getField(String name) throws Exception {
	      if (name.equals(DATASOC)) {
	         return this.doConnect;
	      }
	      return super.getField(name);
	   }
}

 SocketFunction.java

 

package connections;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

import org.w3c.dom.Document;
import org.w3c.dom.events.*;
import net.rim.blackberry.api.mail.event.EventListener;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.browser.field.BrowserContent;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldListener;
import net.rim.device.api.browser.field2.debug.BrowserFieldScript;
import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.script.Scriptable;
import net.rim.device.api.script.ScriptableFunction;

public final class SocketFunction extends ScriptableFunction {
	private BrowserField _browserField
		
    private static String URL = "socket://192.168.1.105:8790;deviceside=false";

	   public Object invoke(Object obj, Object[] args) throws Exception
	   {
		
		   StreamConnection conn = null;
		   	conn = (StreamConnection)Connector.open(URL);
		   	
		   	OutputStreamWriter _out = new OutputStreamWriter(conn.openOutputStream());
		   	String data = "This is a test --->";
		   	_out.write(data);
		   	
		   	InputStreamReader _in = new InputStreamReader(conn.openInputStream());
		   	StringBuffer sb = new StringBuffer();
		   	int c = 0;
	        while (((c = _in.read()) != '\n') && (c != -1)) {
	           sb.append((char) c);
	        }
	
	       // I want execute socketMessage function if server socket push data socketMessage(" + sb.toString() + ")

	                 _in.close();
		   	_out.close();
		   	conn.close();
		   	return UNDEFINED;
		 
	   }

}

 

 

your assistance greatly appreciated.

 

Thanks

 

 

Developer
Posts: 164
Registered: ‎08-26-2010
My Device: Not Specified

Re: Callback function in JavaScript extension [using "call by name" mechanism]

[ Edited ]

 


your assistance greatly appreciated.

 

Thanks

 


For now, i suggest you to focus to the javascript callback method only.

 

So try to simplify your java codes, its function will just call a simple javascript.

Here is my simple version of the ScriptableFunction java codes that you can try:

 

package widgetpackage;

import net.rim.device.api.script.ScriptableFunction;

import net.rim.device.api.browser.field2.BrowserField;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import java.lang.*;

public final class Vibrate extends ScriptableFunction {

	private ScriptableFunction _callback = null;

	
	public Object invoke(Object obj, Object[] args) throws Exception
	{
		
		
		_callback = (ScriptableFunction)args[2];
                                    
                 try {

				
					// Trigger our event callback
					if (_callback != null) {

						//Alert.startVibrate(1000);

						Object[] args2 = new Object[2];
						args2[0] = new String("hellowww");

						_callback.invoke(_callback,args2);

						
					}
				
				 }
				 
				 catch (Exception e) {
					
			}

    

	}

		return UNDEFINED;
		
		
}

 Then in the sjavascript side something like this:

 

<script type="text/javascript">
 
function  ShowMessage(msg) {
 alert(msg);
}

Mywidget.Ext.Testing("var1","var2",ShowMessage); // execute the JS Extension, see that the third parameter is the javascript function above: ShowMessage

</script>

If you have been successfull with this simple codes, next you can try to expand with your more complex httpConnection codes.

Contributor
Posts: 21
Registered: ‎04-23-2011
My Device: 9700
My Carrier: xl

Re: Callback function in JavaScript extension [using "call by name" mechanism]

Thanks all ...

I have success execute javascript function from extention with executeScript()

but come new problem.. cannot receive socketMessage from server.. and respone to long...

bellow..  is my code

 

socketFunction.java

 

package connections;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.ui.component.Dialog;
public final class SocketFunction extends ScriptableFunction {
	private BrowserField _browserField;
	public SocketFunction(BrowserField bfRef)
	{
		this._browserField = bfRef;
	}
	private static String URL = "socket://192.168.1.105:8790;deviceside=false";
    public Object invoke(Object obj, Object[] args) throws Exception
	   {
    	    //String sb = "IDX|10.30|abgx|9500|2|NI|F|KZ|D";
    	    Dialog.alert("start execute socket");
            StreamConnection conn = null;
		   	conn = (StreamConnection)Connector.open(URL);
		   	OutputStreamWriter _out = new OutputStreamWriter(conn.openOutputStream());
		   	String data = "IDX|10.30|abgx|9500|2|NI|F|KZ|D";
		   	_out.write(data);
		   	InputStreamReader _in = new InputStreamReader(conn.openInputStream());
		   	StringBuffer sb = new StringBuffer();
		   	int c = 0;
	        while (((c = _in.read()) != '\n') && (c != -1)) {
	           sb.append((char) c);
	        }
	        Dialog.alert("hasil: "+sb.toString()+"do call execute socketMessage");
	       _browserField.executeScript("socketMessage('"+sb.toString()+"')");
	       Dialog.alert("finis call execute socketMessage");
	       _in.close();
		   _out.close();
		   conn.close();
		   
           return UNDEFINED;
		}

}

 

 

ConnNamespace.java

 

package connections;
import net.rim.device.api.script.Scriptable;
import net.rim.device.api.browser.field2.BrowserField;
public final class ConnNamespace extends Scriptable {
	 //  public static final String socketStatus = "CONNECTED";
	   public static final String DATASOC = "data";
	   private SocketFunction doConnect;
	   public ConnNamespace(BrowserField bfRef) {
		      this.doConnect = new SocketFunction(bfRef);
		   }
	   // The getField() function is called when the 
	   //  dot '.' extender is used on your JavaScript object.
	   public Object getField(String name) throws Exception {
	      if (name.equals(DATASOC)) {
	         return this.doConnect;
	      }
	      return super.getField(name);
	   }
}

 

 

 

 

ConnExtensions.Java

 

package connections;

import org.w3c.dom.Document;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.web.WidgetConfig;
import net.rim.device.api.web.WidgetExtension;
public final class ConnExtensions implements WidgetExtension {
	private BrowserField _bf = null;
	public String[] getFeatureList() {
	        String[] result = new String[1];
	        result[0] = "socket.connection";
	        return result;
	}

    public void loadFeature(String feature, String version,
			   Document doc, ScriptEngine scriptEngine) throws Exception {
			         if (feature == "socket.connection") {
			            scriptEngine.addExtension("socket.connection", new ConnNamespace(_bf));
			            
			         }
	}
    public void register(WidgetConfig arg0, BrowserField arg1) {
		this._bf = arg1;
    }
    public void unloadFeatures(Document arg0) {
		// TODO Auto-generated method stub
    }

}

 

 

Contributor
Posts: 21
Registered: ‎04-23-2011
My Device: 9700
My Carrier: xl

Re: Callback function in JavaScript extension [using "call by name" mechanism]

I ready create permission... domai name socket://192.168.1.105:8790
need permition in extension ??
Whatswrong in my code ??

Developer
Posts: 104
Registered: ‎07-08-2010
My Device: blackberry bold
My Carrier: developer

Re: Callback function in JavaScript extension [using "call by name" mechanism]

[ Edited ]
btnClrAll=new ButtonField("ClrAll")
{
protected boolean invokeAction(int action) 
{
 ScriptableFunction submitSearch = new ScriptableFunction()
 {            
 public Object invoke(Object thiz, Object[] args) throws Exception
	{
	browserField.getScriptEngine().executeScript("clrAllWithinShow()", null);
	return Boolean.FALSE;
    }
};
  return true;
	}
};

 

 

i am trying using this code , but ts not working , i dont know at where i am wrong 

 so please give me solution.

 

 

 

Regards
Vishal Patel
Developer
Posts: 164
Registered: ‎08-26-2010
My Device: Not Specified

Re: Callback function in JavaScript extension [using "call by name" mechanism]


vishal_33 wrote:
btnClrAll=new ButtonField("ClrAll")
{
protected boolean invokeAction(int action) 
{
 ScriptableFunction submitSearch = new ScriptableFunction()
 {            
 public Object invoke(Object thiz, Object[] args) throws Exception
	{
	browserField.getScriptEngine().executeScript("clrAllWithinShow()", null);
	return Boolean.FALSE;
    }
};
  return true;
	}
};

 

 

i am trying using this code , but ts not working , i dont know at where i am wrong 

 so please give me solution.

 

 


just try to use another codes i have given examples above.