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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 382
Registered: ‎05-19-2008
My Device: BlackBerry Torch
My Carrier: AT&T

Can custom widget extension return a complex object?

The alert Sample that is so commonly referenced any practically every doc on how to create a custom extension (http://docs.blackberry.com/en/developers/deliverables/21186/BlackBerry_Widget_SDK-Development_Guide-...) only shows returning a primitive type for the isVibrateSupported field. However, is is possible to return a complex object? Like a defined class that is internal to my custom Extension?

 

Would that mean that the return object itself would have to be a class that implements Scriptable?

 

I don't see why you couldn't return an object with a rich set of methods and fields if they all were implemented the right way with Scriptable and ScriptableFunction. Am I missing something?

 

 

Bill
-------------------------------------------
Check out my book on BlackBerry Development for Java.
And my other really really old book
My Apps: FlashKids
Developer
Posts: 588
Registered: ‎05-29-2009
My Device: 9100

Re: Can custom widget extension return a complex object?

I think this should work out. I have passed complex type objects to an exception, by making this type Scriptable, so I guess It would be something similar.
Developer
Posts: 84
Registered: ‎06-26-2008
My Device: Not Specified

Re: Can custom widget extension return a complex object?

What i do is return string object with json format. i transform to json on the html/js side.

 

 

Developer
Posts: 382
Registered: ‎05-19-2008
My Device: BlackBerry Torch
My Carrier: AT&T

Re: Can custom widget extension return a complex object?

passing out  JSON is an interesting idea. I'm not sure why I didn't think of that! Smiley Happy I do really like the idea of a complex object though because you can have methods that operate on the instance data of the objects (I think).

 

So you return a scriptable car object and the car has an unLock ScriptableFunction on it that could access some private member (VIN) inside the car object. Thats my goal, anyway.

 

Bill
-------------------------------------------
Check out my book on BlackBerry Development for Java.
And my other really really old book
My Apps: FlashKids
Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Can custom widget extension return a complex object?

For inspiration you can always check out the full source code for our official WebWorks APIs

 

https://github.com/blackberry/WebWorks/tree/master/api

 

Or check out the community built APIs that also have some examples of this

 

https://github.com/blackberry/WebWorks-Community-APIs/tree/master/Smartphone

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Trusted Contributor
Posts: 227
Registered: ‎09-03-2010
My Device: Not Specified

Re: Can custom widget extension return a complex object?

 

I made an example, I hope it's helpful.

 

how use:

 

<script type="text/javascript">

function greet(){
	var values = {
				name:"Carlos David",
				lastname:"Grullard Almonte"
			}
	
	hello.greet(values,function(data){
		alert(data.full_name)
	})
	
}

greet()


</script>

 

code extension:

 

 

package hello;
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 helloExtension implements WidgetExtension {
      public String[] getFeatureList() {
      String[] result = new String[1];
      result[0] = "hello";
      return result;
   }

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

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

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

 

package hello;
import net.rim.device.api.script.Scriptable;
import net.rim.device.api.script.ScriptableFunction;
public final class hellofunction extends ScriptableFunction {

	private ScriptableFunction _callback  = null;
   public Object invoke(Object obj, Object[] args) throws Exception
   {
	   Scriptable values  = (Scriptable) args[0]; 
	   
	   _callback =(ScriptableFunction) args[1];
	   
	   _callback.invoke(this, new Object[] {new ObjectResponse(
			   						values.getField("name").toString(),
			   						values.getField("lastname").toString())
	   } );
	   
   return UNDEFINED;
   }
}

 

package hello;
import net.rim.device.api.script.Scriptable;
public final class helloNamespace extends Scriptable {
   
   public static final String FIELD_GREET = "greet";
   private hellofunction _greet;

   public helloNamespace() {
      this._greet = new hellofunction();
   }

   // 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(FIELD_GREET)) {
         return this._greet;
      }
      return super.getField(name);
   }
}

 

package hello;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

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

public class ObjectResponse extends Scriptable {

	public static final String FULL_NAME = "full_name";
	private Hashtable _fields;

	public ObjectResponse(String name, String lastname) {
		_fields = new Hashtable();
		_fields.put(FULL_NAME, new String(name + ' ' + lastname));
	}

	/**
	 * @see net.rim.device.api.script.Scriptable#enumerateFields(Vector)
	 */
	public void enumerateFields(Vector v) {
		if (!_fields.isEmpty()) {
			for (Enumeration e = _fields.keys(); e.hasMoreElements();) {
				v.addElement(e.nextElement());
			}
		}
	}

	/**
	 * see net.rim.device.api.script.Scriptable#getElementCount()
	 */
	public int getElementCount() {
		return _fields.size();
	}

	/**
	 * @see net.rim.device.api.script.Scriptable#getField(String)
	 */
	public Object getField(String fieldName) throws Exception {
		Object field = _fields.get(fieldName);
		if (field == null) {
			return super.getField(fieldName);
		}
		return field;
	}

}