Search code examples
javagwtgwt2

How to create a JsArray as JavaScriptObject


i need a JavaScriptObject what contains a JsArray and a String. That JsArray should have all functionalities like the normal GWT`s JsArray like push(), pop() etc. but how to create that? I have created something what i mean but it doesnt work

public class Tour extends JavaScriptObject {

protected Tour() {
    // TODO Auto-generated constructor stub
}

public static Tour getInstance(){
    return JavaScriptObject.createArray().cast();
}

public final native void setTourName(String name)/*-{
    this.name = name;
}-*/;

public final native String getTourName()/*-{
    return this.name;
}-*/;

public final native JsArray<JavaScriptObject> getArray() /*-{
    return this.array;
}-*/;

public final native void setJsArray(JsArray<JavaScriptObject> array)/*-{
    this.array = array;
}-*/;

public final native void push(JavaScriptObject obj)/*-{
    this.array.push(obj);
}-*/;

public final native int getLength()/*-{
    this.array.length;
}-*/;

}


Solution

  • public class Tour extends JavaScriptObject {
    
        protected Tour() {
        }
    
        public static Tour getInstance() {
            return getInstance(null, null);
        }
    
        public static Tour getInstance(String name) {
            return getInstance(name, null);
        }
    
        public static native Tour getInstance(String name, JsArray<JavaScriptObject> array) /*-{
            return {
                name : name,
                array : array || []
            };
        }-*/;
    
        // the rest of your class stays untouched...
    
        public final native void setTourName(String name)/*-{
            this.name = name;
        }-*/;
    
        public final native String getTourName()/*-{
            return this.name;
        }-*/;
    
        public final native JsArray<JavaScriptObject> getArray() /*-{
            return this.array;
        }-*/;
    
        public final native void setJsArray(JsArray<JavaScriptObject> array)/*-{
            this.array = array;
        }-*/;
    
        public final native void push(JavaScriptObject obj)/*-{
            this.array.push(obj);
        }-*/;
    
        public final native int getLength()/*-{
            this.array.length;
        }-*/;
    }