I am working on a new class and I want to expose instance fields and methods via a prototype via JSNI so I can call from third party script into my GWT code like this.
var mySimpleObject = new Simple("test"); test.callMyName(); I have been working through a series of examples that build on each other but I am not sure that my assumptions about why the ones that work really work and why the ones that fail really fail. Here they are -- JSNI experts please comment! Thanks! package com.test.client; public class Simple { public String name; public Simple(String name) { this.name = name; } public static Simple createSimple(String name) { return new Simple(name); } // this works becuase you are passing in the Simple reference public native void callMyNameWorks(Simple myInstance) /*-{ $wnd.callMyName = function(){ alert( [EMAIL PROTECTED]::name); } alert( [EMAIL PROTECTED]::name); $wnd.callMyName(); }-*/; // this works because we COPY the reference and store it in a closure. public native void callMyNameWorks() /*-{ var myInstance = this; $wnd.callMyName = function(){ alert( [EMAIL PROTECTED]::name); } //"this" in GWT entrypoint is the Simple object that called this method. alert( [EMAIL PROTECTED]::name); $wnd.callMyName(); }-*/; // this broken because "this" is not the right object when our new function // gets called. public native void callMyNameHalfBroken() /*-{ $wnd.callMyName = function(){ //this will break because at "this" is actually the window object now (or who called the method) alert( [EMAIL PROTECTED]::name); } //"this" works b/c GWT entrypoint is the Simple object that called this method. alert( [EMAIL PROTECTED]::name); $wnd.callMyName(); }-*/; public static native void callMyNameStaticGlobalWorks() /*-{ //--------------------------------------- //NOTE: this RETURNS the GWT Simple Object not a js object //--------------------------------------- $wnd.jsSimple = function (name){ return @com.siemens.soarian.sf.uiarch.client.Simple::createSimple(Ljava/lang/ String;)(name) } //myObject must be an instance of the GWT Simple Object $wnd.callMyNameGlobal = function(myGwtJavaObject){ alert([EMAIL PROTECTED]::name); } var myJsSimple = new $wnd.jsSimple("testing"); //works bc we pass in the actual GWT Java Simple object $wnd.callMyNameGlobal(myJsSimple); }-*/; public static native void callMyNameStaticGlobalBroken() /*-{ debugger; //--------------------------------------- //NOTE: effectively assigns the value of var name = javascript instance of gwt java object //if you look at the object in debug you will see name.instance is now defined. //name.instance is a reference to the gwt object //--------------------------------------- $wnd.jsSimple = function (name){ this.instance = @com.siemens.soarian.sf.uiarch.client.Simple::createSimple(Ljava/lang/ String;)(name); } //myObject must be an instance of the GWT Simple Object $wnd.callMyNameGlobal = function(myobject){ alert([EMAIL PROTECTED]::name); } //debug shows that there is now a name.instance defined; var myJsSimple = new $wnd.jsSimple("testing"); $wnd.callMyNameGlobal(myJsSimple); }-*/; public static native void callMyNameStaticLocalBroken() /*-{ debugger; //--------------------------------------- //NOTE: effectively assigns the value of var name = javascript instance of gwt java object //if you look at the object in debug you will see name.instance is now defined. //name.instance is a reference to the gwt object //--------------------------------------- $wnd.jsSimple = function (name){ this.instance = @com.siemens.soarian.sf.uiarch.client.Simple::createSimple(Ljava/lang/ String;)(name); } //store a copy of the prototype reference to use later var _= $wnd.jsSimple.prototype; _.callMyNameLocal = function(){ //who is this when its called? it won't be a GWT Java Simple object!!! //it is actually the javascript object -- So the myInstance[blah blah] will be undefined var myInstance = this; alert([EMAIL PROTECTED]::name); } var myJsSimple = new $wnd.jsSimple("testing"); myJsSimple.callMyNameLocal(); }-*/; public static native void callMyNameStaticLocalWorks1() /*-{ debugger; //--------------------------------------- //NOTE: effectively assigns the value of var name = javascript instance of gwt java object //if you look at the js object in debug you will see name.instance is now defined. //name.instance is a reference to the gwt object //--------------------------------------- $wnd.jsSimple = function (name){ this.instance = @com.siemens.soarian.sf.uiarch.client.Simple::createSimple(Ljava/lang/ String;)(name); } //store a copy of the prototype reference to use later var _= $wnd.jsSimple.prototype; _.callMyNameLocal = function(){ //is say "this" then will be broken because when method is called the this is the js object //this.instance = the java object alert([EMAIL PROTECTED]::name); } var myJsSimple = new $wnd.jsSimple("testing"); myJsSimple.callMyNameLocal(); }-*/; public static native void callMyNameStaticLocalWorks2() /*-{ debugger; //--------------------------------------- //NOTE: effectively assigns the value of var name = javascript instance of gwt java object //if you look at the object in debug you will see name.instance is now defined. //name.instance is a reference to the gwt object //--------------------------------------- $wnd.jsSimple = function (name){ this.instance = @com.siemens.soarian.sf.uiarch.client.Simple::createSimple(Ljava/lang/ String;)(name); } //store a copy of the prototype reference to use later var _= $wnd.jsSimple.prototype; _.callMyNameLocal = function(){ //make a copy of the reference the gwt java object so we can use it later when this method //is actually called since this.instance has the right obj ref when the object is created? var myInstance = this.instance; alert([EMAIL PROTECTED]::name); } var myJsSimple = new $wnd.jsSimple("testing"); myJsSimple.callMyNameLocal(); }-*/; } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to Google-Web-Toolkit@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---