Thank you. I have in the mean time got some simple example working, but with a somewhat different "design"; I basically wrote my type in JavaScript, and used JavaScriptObject overlay types to make it look like a Java Type, so I"duplicated" the methods in the Java code, using JSNI, rather then "wrapping" it, but idk if this "scales" (from your example, I see a lot of potential to reduce the size of doExportType()):
public class SomeDataType extends JavaScriptObject {
/** Are we export yet? */
private static boolean typeExported;
/** "Registers" (export) this type in JavaScript, so it can be used from there too. */
private static final native void doExportType() /*-{
if (!$wnd.com) {
$wnd.com = {};
}
var com = $wnd.com
if (!com.company) {
com.company= {};
}
if (!com.company.util) {
com.company.util = {};
}
if (!com.company.util.client) {
com.company.util.client = {};
}
function SomeDataType() {
this.age = 42;
}
SomeDataType.prototype.name = function() {
return "John";
}
SomeDataType.prototype.setAge = function(newAge) {
this.age = newAge;
}
SomeDataType.prototype.getAge = function() {
return this.age;
}
com.company.util.client.SomeDataType = SomeDataType;
}-*/;
/** "Registers" (export) this type in JavaScript, so it can be used from there too. */
public static void exportType() {
if (!typeExported) {
typeExported = true;
doExportType();
}
}
public static SomeDataType create() {
exportType();
return (SomeDataType) doCreate().cast();
}
/** Does the instance initialization. */
private static final native JavaScriptObject doCreate() /*-{
return new $wnd.com.company.util.client.SomeDataType();
}-*/;
/** Constructor; must always be protected without parameter for overlay types. */
protected SomeDataType() {
// NOP
}
public final native String name() /*-{
return this.name();
}-*/;
public final native int getAge() /*-{
return this.getAge();
}-*/;
public final native void setAge(int newAge) /*-{
this.setAge(newAge);
}-*/;
}.
I am hoping that I could reuse the same code to allow cross-application communication, as would be required, for example, if using WebWorkers.
Note that since my "domain" Java code itself will be generated from Xtend (but not in this example), I might just be able to eventually generate the boiler-plate JSNI code myself, if gwt-exporter takes to long to be updated.
-- public class SomeDataType extends JavaScriptObject {
/** Are we export yet? */
private static boolean typeExported;
/** "Registers" (export) this type in JavaScript, so it can be used from there too. */
private static final native void doExportType() /*-{
if (!$wnd.com) {
$wnd.com = {};
}
var com = $wnd.com
if (!com.company) {
com.company= {};
}
if (!com.company.util) {
com.company.util = {};
}
if (!com.company.util.client) {
com.company.util.client = {};
}
function SomeDataType() {
this.age = 42;
}
SomeDataType.prototype.name = function() {
return "John";
}
SomeDataType.prototype.setAge = function(newAge) {
this.age = newAge;
}
SomeDataType.prototype.getAge = function() {
return this.age;
}
com.company.util.client.SomeDataType = SomeDataType;
}-*/;
/** "Registers" (export) this type in JavaScript, so it can be used from there too. */
public static void exportType() {
if (!typeExported) {
typeExported = true;
doExportType();
}
}
public static SomeDataType create() {
exportType();
return (SomeDataType) doCreate().cast();
}
/** Does the instance initialization. */
private static final native JavaScriptObject doCreate() /*-{
return new $wnd.com.company.util.client.SomeDataType();
}-*/;
/** Constructor; must always be protected without parameter for overlay types. */
protected SomeDataType() {
// NOP
}
public final native String name() /*-{
return this.name();
}-*/;
public final native int getAge() /*-{
return this.getAge();
}-*/;
public final native void setAge(int newAge) /*-{
this.setAge(newAge);
}-*/;
}.
I am hoping that I could reuse the same code to allow cross-application communication, as would be required, for example, if using WebWorkers.
Note that since my "domain" Java code itself will be generated from Xtend (but not in this example), I might just be able to eventually generate the boiler-plate JSNI code myself, if gwt-exporter takes to long to be updated.
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment