BrowserComponent.createJSProxy(java.lang.String)
to create a Javascript proxy object instead.public class JSObject extends Object
NOTE: The com.codename1.javascript
package is now deprecated. The preferred method of
Java/Javascript interop is to use BrowserComponent.execute(java.lang.String)
, BrowserComponent.execute(java.lang.String, com.codename1.util.SuccessCallback)
,
BrowserComponent.executeAndWait(java.lang.String)
, etc.. as these work asynchronously (except in the XXXAndWait() variants, which
use invokeAndBlock() to make the calls synchronously.
A JSObject is associated with a particular JavascriptContext
and it is backed
by a Javascript object inside the javascript context. This object acts as a
proxy to call methods and access properties of the actual javascript object.
All return values for Javascript calls in the JavascriptContext
will be
converted to the appropriate Java type. Javascript objects will automatically
be wrapped in a JSObject proxy.
JSObject provides get(String)
and set(String,Object)
methods to get and set properties on the
object. E.g.
obj.set("name", "Steve");
obj.set("age", 12);
String name = (String)obj.get("name"); // Steve
Double age = (Double)obj.get("age"); // 12.0
The return value of get(String)
will be one of Double, String, Boolean, or JSObject
depending on the type of Javascript value that is being returned. get(String)
requires
you to cast the return value to the correct type, which is a bit a pain. Luckily,
JSObject provides a set of typed getter methods that will automatically cast to
particular types:
getInt() | Returns int |
getString() | Returns String |
getDouble() | Returns double |
getObject() | Returns JSObject |
getBoolean() | Returns boolean |
JSObject can wrap Javascript arrays also. You can retrieve the indexed
properties of the array using indexed versions of get(int)
and set(int,Object)
(i.e. they
take an int as their first parameter instead of a String). There are also
typed versions of the indexed get(int)
method to allow directly returning
values of the correct type without having to type cast
JSObject colors = ctx.get("['red','green','blue']");
int len = colors.getInt("length");
for ( int i=0; i< len; i++ ){
System.out.println("Color "+i+" is "+colors.getString(i));
}
JSObject allows you to call Javascript object methods on the wrapped
Javascript object via the its call(String,Object[])
method. It takes the name of the
method (i.e. property name that stores the function), and an array of
parameters to pass to the method.
JSObject document = (JSObject)context.get("document");
// Call document.writeln("Hello world");
document.call("writeln", new Object[]{"Hello world"});
Since, in Javascript, functions are objects, it is possible to wrap a function
with a JSObject object also. You can then call the function using the alternate
version of the #call(JSObject,Object[])
method.
JSObject window = (JSObject)context.get("window");
// reference to the window object so that we can pass it as the context
// of the function call.
JSObject myfunc = (JSObject)context.get("function(a,b){ return a+b}");
Double result = (Double)myfunc.call(window, new Object[]{new Integer(1), new Integer(2)});
The JSFunction
interface allows you to implement functions in Java that can
be called from Javascript. You can assign any JSFunction
object to be a member
method of an existing JSObject via the JSObject.set()
method. Then the function
can be called from javascript just like any other Javascript method. JSFunction
methods are called asynchronously from Javascript to prevent deadlocks. If you
require a return value to Javascript, you can do that by passing a callback
function which is called by the JSFunction with some parameters.
The following example, adds a camera object to the Javascript environment that has a capture() method, which can be used to capture images using the device's camera:
// Create a new Javascript object "camera"
final JSObject camera = (JSObject)ctx.get("{}");
// Create a capture() method on the camera object
// as a JSFunction callback.
camera.set("capture", new JSFunction(){
public void apply(JSObject self, final Object[] args) {
Display.getInstance().capturePhoto(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
String imagePath = (String)evt.getSource();
// Get the callback function that was provided
// from javascript
JSObject callback = (JSObject)args[0];
ctx.call(
callback, // The function
camera, // The "this" object
new Object[]{"file://"+imagePath} // Parameters
);
}
});
}
});
// Add the camera object to the top-level window object
ctx.set("window.camera", camera);
We can then capture photos directly from Javascript using a function similar to the following:
camera.capture(function(url){
if ( url == null ){
// No image was captured
return;
}
// Fetch the preview <img> tag.
var image = document.getElementById('preview-image');
// Set the preview URL to the image that was taken.
image.src = url;
});
Constructor and Description |
---|
JSObject(JavascriptContext context,
String expr)
Deprecated.
Constructor for a JSObject.
|
Modifier and Type | Method and Description |
---|---|
Object |
call(Object... params)
Deprecated.
Calls the object as a function statically.
|
Object |
call(String key)
Deprecated.
Calls a method on the underlying Javascript object synchronously with no arguments.
|
Object |
call(String key,
Object[] params)
Deprecated.
Calls a method on the underlying Javascript object.
|
void |
callAsync(Object[] params,
Callback callback)
Deprecated.
Calls the object as a function statically.
|
void |
callAsync(Object[] params,
SuccessCallback callback)
Deprecated.
Calls the object as a function statically.
|
void |
callAsync(String key,
Callback callback)
Deprecated.
Calls a method on the underlying Javascript object asynchronously with no arguments.
|
void |
callAsync(String key,
Object[] params,
Callback callback)
Deprecated.
Calls a method on the underlying Javascript object asynchronously.
|
void |
callAsync(String key,
Object[] params,
SuccessCallback callback)
Deprecated.
Calls a method on the underlying Javascript object asynchronously.
|
void |
callAsync(String key,
SuccessCallback callback)
Deprecated.
Calls a method on the underlying Javascript object asynchronously with no arguments.
|
double |
callDouble(String key)
Deprecated.
Calls a method on the underlying Javascript object that returns a double.
|
void |
callDoubleAsync(String key,
Callback<Double> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns a double.
|
void |
callDoubleAsync(String key,
SuccessCallback<Double> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns a double.
|
int |
callInt(String key)
Deprecated.
Calls a method on the underlying Javascript object that returns an int.
|
void |
callIntAsync(String key,
Callback<Integer> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns an int.
|
void |
callIntAsync(String key,
SuccessCallback<Integer> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns an int.
|
JSObject |
callObject(String key)
Deprecated.
Calls a method on the underlying Javascript object that returns a Javascript object.
|
void |
callObjectAsync(String key,
Callback<JSObject> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns a Javascript object.
|
void |
callObjectAsync(String key,
SuccessCallback<JSObject> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns a Javascript object.
|
String |
callString(String key)
Deprecated.
Calls a method on the underlying Javascript object that returns a String.
|
void |
callStringAsync(String key,
Callback<String> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns a String.
|
void |
callStringAsync(String key,
SuccessCallback<String> callback)
Deprecated.
Calls a method on the underlying Javascript object that returns a String.
|
Object |
get(int index)
Deprecated.
This method is useful only for JSObjects that encapsulate Javascript arrays.
|
Object |
get(String key)
Deprecated.
Returns a member variable of the Javascript object.
|
boolean |
getBoolean(int index)
Deprecated.
Wrapper for get(int) for indexed boolean values.
|
boolean |
getBoolean(String key)
Deprecated.
Wrapper around get() to return a boolean.
|
double |
getDouble(int index)
Deprecated.
Wrapper for get(int) for indexed double values.
|
double |
getDouble(String key)
Deprecated.
Wrapper around get() to return a double.
|
int |
getInt(int index)
Deprecated.
Wrapper for get(int) for indexed int values.
|
int |
getInt(String key)
Deprecated.
Wrapper around get() to return an int
|
JSObject |
getObject(int index)
Deprecated.
Wrapper for get(int) for indexed object values.
|
JSObject |
getObject(String key)
Deprecated.
Wrapper around the get() method to return a JSObject.
|
String |
getString(int index)
Deprecated.
Wrapper for get(int) for indexed string values.
|
String |
getString(String key)
Deprecated.
Wrapper around get() to return a String.
|
void |
set(int index,
Object js)
Deprecated.
Sets an indexed value on this object synchronously (assuming this object is a Javascript array).
|
void |
set(int index,
Object js,
boolean async)
Deprecated.
Sets an indexed value on this object (assuming this object is a Javascript array).
|
void |
set(String key,
Object js)
Deprecated.
Sets a property on this object.
|
void |
set(String key,
Object js,
boolean async)
Deprecated.
Sets a property on the underlying Javascript object.
|
void |
setBoolean(int index,
boolean value)
Deprecated.
Sets an indexed value on this array to a boolean synchronously.
|
void |
setBoolean(int index,
boolean value,
boolean async)
Deprecated.
Sets an indexed value on this array to a boolean.
|
void |
setBoolean(String key,
boolean value)
Deprecated.
Sets a property on this object to a boolean value synchronously.
|
void |
setBoolean(String key,
boolean value,
boolean async)
Deprecated.
Sets a property on this object to a boolean value.
|
void |
setDouble(int index,
double value)
Deprecated.
Sets an indexed value on this array to a double synchronously.
|
void |
setDouble(int index,
double value,
boolean async)
Deprecated.
Sets an indexed value on this array to a double.
|
void |
setDouble(String key,
double value)
Deprecated.
Sets a property on this object to a double value synchronously.
|
void |
setDouble(String key,
double value,
boolean async)
Deprecated.
Sets a property on this object to a double value.
|
void |
setInt(int index,
int value)
Deprecated.
Sets an indexed value on this array to an integer synchronously.
|
void |
setInt(int index,
int value,
boolean async)
Deprecated.
Sets an indexed value on this array to an integer.
|
void |
setInt(String key,
int value)
Deprecated.
Sets a property on this object to an integer value synchronously.
|
void |
setInt(String key,
int value,
boolean async)
Deprecated.
Sets a property on this object to an integer value.
|
String |
toJSPointer()
Deprecated.
Returns a Javascript variable name for the underlying Javascript object.
|
String |
toString()
Deprecated.
Returns the toString from the JavaScript object effectively the equivalent of
callString("toString") |
public JSObject(JavascriptContext context, String expr)
// Create a JavascriptContext for a browser component
JavascriptContext ctx = new JavascriptContext(browserComponent);
// Get reference to the window object
JSObject window = new JSObject(ctx, "window");
// This is equivalent to
window = (JSObject)ctx.get("window");
context
- The javascript context in which this object is being created.expr
- A javascript expression that resolves to a Javascript Object.public Object get(String key)
E.g., suppose you have a Javascript object myCar whose JSON representation is
{ make : 'Ford', model : 'Escort', 'year' : 1989}
Then the JSObject proxy for this object could call:
String model = (String)myCar.get("model");
And this would return "Ford".
JSObject document = (JSObject)ctx.get("document");
// Get document title
String title = document.get("title");
// Since the "title" property is a string, get() returns a String.
// We could equivalently use
title = document.getString("title");
// Get a grandchild property
Double titleLength = (Double)document.get("title.length");
// Since length is an integer, it is probably easier to use getInt()
int titleLengthInt = document.getInt("title.length");
// Get a child object
JSObject body = (JSObject)document.get("body");
// Since "body" is a Javascript object, it is probably easier to use
// getObject()
JSObject body2 = document.getObject("body");
// Get a method as a function object
JSObject open = (JSObject) document.get("open");
// Call the open() method, with document as "this"
open.call(document, new Object[]{}); // Takes no parameters
// Equivalently we could have called open via the document object
document.call("open", new Object[]{});
key
- The name of the property to retrieve on this object.public String getString(String key)
key
- The name of the property in the object to retrieve. Value of this
property must be a string.public int getInt(String key)
key
- The name of the property in the object to retrieve. Value of this
property must be an integer.public double getDouble(String key)
key
- The name of the property in the object to retrieve. Value of this property
must be a number.public boolean getBoolean(String key)
key
- The name of the property in the object to retrieve. Value of this property
must be a boolean.public JSObject getObject(String key)
key
- The name of the property in the object to retrieve. Value of this property
must be a Javascript object or function.public Object get(int index)
JSObject colors = ctx.get("['red','green','blue']");
String red = (String)colors.get(0);
String green = (String)colors.get(1);
String blue = (String)colors.get(2);
It may be more convenient to use the typed wrapper methods so that you don't have to typecast the values. E.g.
String red = colors.getString(0);
String green = colors.getString(1);
String blue = colors.getString(2);
JSObject colors = ctx.get("['red','green','blue']");
int len = colors.getInt("length");
for ( int i=0; i< len; i++ ){
System.out.println("Color "+i+" is "+colors.getString(i));
}
index
- The index of the entry within the array to return.public String getString(int index)
index
- The index within an Array object whose value to retrieve.public int getInt(int index)
index
- The index within the Array object whose value to retrieve.public double getDouble(int index)
index
- The index within the Array object whose value to retrieve.public boolean getBoolean(int index)
index
- The index within the Array object whose value to retrieve.public JSObject getObject(int index)
index
- The index within the Array object whose value to retrieve.public void set(String key, Object js, boolean async)
this[key] = js;
.key
- The name of the property to set on the current object.js
- The value of the property. This value should be provided as a
Java value and it will be converted to the appropriate Javascript value.
See @ref JavascriptContext.set() for a conversion table of the Java to
Javascript type conversions.async
- If this flag is set, then the call will be asynchronous (will not wait for command to complete before continuing execution).public void set(String key, Object js)
this[key] = value;
.key
- The name of the property.js
- A value for the property. This may be a primitive type, a JSObject, or a JSFunction.set(java.lang.String, java.lang.Object, boolean)
public void setInt(String key, int value, boolean async)
this[key] = value;
.key
- The property name to set.value
- The value to assign to the property.async
- True if you want this call to be asynchronous.public void setInt(String key, int value)
this[key] = value;
.key
- The name of the property to set.value
- The integer value of to set.public void setDouble(String key, double value, boolean async)
this[key] = value;
.key
- The name of the property to setvalue
- The value to set.async
- True if you want this call to be asynchronous.public void setDouble(String key, double value)
this[key] = value;
.key
- The name of the property to setvalue
- The value to set.public void setBoolean(String key, boolean value, boolean async)
this[key] = value;
.key
- The name of the property to setvalue
- The value to set.async
- True if you want this call to be asynchronous.public void setBoolean(String key, boolean value)
this[key] = value;
.key
- The name of the property to setvalue
- The value to set.public void set(int index, Object js, boolean async)
this[index] = js;
.index
- The index to set.js
- The object to set. This may be a primitive type, a String, a JSObject, or a JSFunction.async
- True to make this call asynchronously.public void set(int index, Object js)
this[index] = js;
.index
- The index to set.js
- The object to set. This may be a primitive type, a String, a JSObject, or a JSFunction.public void setInt(int index, int value, boolean async)
this[index] = value;
.index
- The index within this array to set.value
- The value to set.async
- True to make this call asynchronous.public void setInt(int index, int value)
this[index] = value;
.index
- The index within this array to set.value
- The value to set.public void setDouble(int index, double value, boolean async)
this[index] = value;
.index
- The index within this array to set.value
- The value to set.async
- True to make this call asynchronous.public void setDouble(int index, double value)
this[index] = value;
.index
- The index within this array to set.value
- The value to set.public void setBoolean(int index, boolean value, boolean async)
this[index] = value;
.index
- The index within this array to set.value
- The value to set.async
- True to make this call asynchronous.public void setBoolean(int index, boolean value)
this[index] = value;
.index
- The index within this array to set.value
- The value to set.public String toJSPointer()
public Object call(String key, Object[] params)
key
- The name of the method to call.params
- Array of parameters to pass to the method. These will be
converted to corresponding Javascript types according to the translation
table specified in JavascriptContext.set(String,Object)
JavascriptContext.get(String)
public void callAsync(String key, Object[] params, Callback callback)
key
- The name of the method to call.params
- Array of parameters to pass to the method. These will be
converted to corresponding Javascript types according to the translation
table specified in JavascriptContext.set(String,Object)
callback
- Callback to be called when the method call is completed.public void callAsync(String key, Object[] params, SuccessCallback callback)
key
- The name of the method to call.params
- Array of parameters to pass to the method. These will be
converted to corresponding Javascript types according to the translation
table specified in JavascriptContext.set(String,Object)
callback
- Callback to be called when the method call is completed.public Object call(String key)
key
- The name of the method.public void callAsync(String key, Callback callback)
key
- The name of the method.callback
- Callback to be called with the return value.public void callAsync(String key, SuccessCallback callback)
key
- The name of the method.callback
- Callback to be called with the return value.public int callInt(String key)
key
- The name of the method.public void callIntAsync(String key, Callback<Integer> callback)
key
- The name of the method.callback
- Callback to handle the return value.public void callIntAsync(String key, SuccessCallback<Integer> callback)
key
- The name of the method.callback
- Callback to handle the return value.public double callDouble(String key)
key
- The name of the method.public void callDoubleAsync(String key, Callback<Double> callback)
key
- The name of the method.callback
- Callback to handle the return value.public void callDoubleAsync(String key, SuccessCallback<Double> callback)
key
- The name of the method.callback
- Callback to handle the return value.public String callString(String key)
key
- The name of the method.public void callStringAsync(String key, Callback<String> callback)
key
- The name of the method.callback
- Callback to handle the return value.public void callStringAsync(String key, SuccessCallback<String> callback)
key
- The name of the method.callback
- Callback to handle the return value.public JSObject callObject(String key)
key
- The name of the method.public void callObjectAsync(String key, Callback<JSObject> callback)
key
- The name of the method.callback
- Callback to handle the return value.public void callObjectAsync(String key, SuccessCallback<JSObject> callback)
key
- The name of the method.callback
- Callback to handle the return value.public Object call(Object... params)
E.g.
JSObject alert = (JSObject)ctx.get("window.alert");
alert.call(new Object[]{"An alert message"});
The above gets a reference to the alert() function (remember functions are objects in Javascript). Then it calls it via Java, passing it a single string parameter. This is equivalent to the following Javasript:
alert("An alert message");
params
- The parameters to pass to the function. These will be converted
to the appropriate Javascript type.public void callAsync(Object[] params, Callback callback)
E.g.
JSObject alert = (JSObject)ctx.get("window.alert");
alert.call(new Object[]{"An alert message"});
The above gets a reference to the alert() function (remember functions are objects in Javascript). Then it calls it via Java, passing it a single string parameter. This is equivalent to the following Javasript:
alert("An alert message");
params
- The parameters to pass to the function. These will be converted
to the appropriate Javascript type.callback
- The result of the javascript function call converted to the appropriate
Java type and passed to the callback.public void callAsync(Object[] params, SuccessCallback callback)
E.g.
JSObject alert = (JSObject)ctx.get("window.alert");
alert.call(new Object[]{"An alert message"});
The above gets a reference to the alert() function (remember functions are objects in Javascript). Then it calls it via Java, passing it a single string parameter. This is equivalent to the following Javasript:
alert("An alert message");
params
- The parameters to pass to the function. These will be converted
to the appropriate Javascript type.callback
- The result of the javascript function call converted to the appropriate
Java type and passed to the callback.