On Friday, September 21, 2012 1:16:56 PM UTC+2, js402882 wrote:
>
> Is there any way get info if JavaScriptObject is one of defined overlay 
> objects? 
> Example:
> I have native factory method which creates JavaScriptObjects:
>
> class WeirdFactory
> {
>     native JavaScriptObject create(String type)
>     {
>         if (type == "car") return { name: "e46", maker: "bmw", seats: "4"}
>         else if (type == "table") return { material: "wood", color: 
> "brown"}
>         else if (type == "type i forgot to crate overlay object for") 
> return { id: "someId", name: "someName" }
>
>         return null;
>     }
> }
>
> class Car extends JavaScriptObject
> {
>     native String name() /*-{ return this.name; }-*/;
>     native String maker() /*-{ return this.maker; }-*/; 
>     native int seats() /*-{ return this.seats; }-*/; 
> }
>
> class Table extends JavaScriptObject
> {
>     native String material() /*-{ return this.material; }-*/;
>     native String color() /*-{ return this.color; }-*/; 
> }
>
> Now I want to use this factory to create overlay objects:
>
> <T> void getValue(Class<T> returnType, Callback<T> callback, String type)
> {
>     JavaScriptObject value = weirdFactory.create(type);
>
>     // Here I need something like
>     // if (!JavaScriptObject.isDefined(value))
>     // {
>     //     *handleUndefinedJSO(value);* // or throw new 
> IllegalArgumentException("Undefined overlay object")
>     // }
>     
>     T returnValue = (T)value;
> *
> *
> *    *callback.returned(returnValue)
> }
>
> I need somehow check if *value* JavaScriptObject has defined custom 
> overlay object (... extends JavaScriptObject)
> In pure Java I would catch ClassCastExcetion. But this does not work with 
> JavaScriptObject which simply casts to anything (from it's nature, it's 
> JavaScript object in the end after compilation).
>
> Does anybody have idea how to get info if JavaScriptObject is one of 
> defined overlay objects? 
>
>
There's nothing like "defined overlays for a given JS object". Overlays are 
only a Java API to access a native JS object.
For instance, "Car car = weirdFactory.create("table")" is perfectly legal, 
and car's getters will simply return null/undefined. And then you can do 
"Table table = car.cast()".

You don't have a notion of "type" on your created objects, so you won't 
have that in Java either. I mean, in JS, with the same factory, how would 
you tell if you have a "table" or a "car"? if you had constructor functions 
for the objects (new Table, new Car), you could do instanceof checks (in 
JS). Or you could have a "type" property on your objects, so you could 
getType() in Java to do conditional processing.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-web-toolkit/-/vwb8i-sTz8EJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to