First of all thanks for reply.
I understand that.
My problem is that I don't know what object is created in native code. Maybe I oversimplified my real use case in the name of clarity.
What I want to do is JSON RPC client for GWT. The aim is provide simple API which will only require the user to define return types as JSO overlays and definition of remote interface. In other words API should be as near as possible to the GWT RPC. That said I don't know (in native code) of what type the created object is:
- good case is that it's correct JSON object which has defined JSO overlay;
- bad case is that it's something else (string, wrong/unexpected JSON object, ...), in that case I need to generate meaningful error
What I need to solve is how to know that response is wrong. Now, only solution I see is some sort of JSON schema validation in native code - this would work, but it would require additional work from user of API (define schema of good response). But that's I want to avoid.
If you have better idea and willing to share, please do :)
On Friday, September 21, 2012 2:33:30 PM UTC+2, Thomas Broyer wrote:
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/-/o6rilsP8w74J.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
No comments:
Post a Comment