mvairavan
mvairavan

Reputation: 129

GWT: creating associated jsarray

I want to create a js array of type

Name(
{title : "Mr.",firstname : "Bill",lastname : "Gates"},
{title : "Mr.",firstname : "Bill",lastname : "Gates"},
{title : "Mr.",firstname : "Bill",lastname : "Gates"}
)

So basically i want to create associated array. All the examples are like converting javascript array to java but in my case i want the other way round. I will be filling this array from java.

JSArray and JsMixedArray seems to be doing this but i could figure out how to add to them.

Upvotes: 3

Views: 2349

Answers (3)

Renaud
Renaud

Reputation: 4668

You can create empty JavaScriptObject from Java but you cannot populate them from there, so use the dark side of the force:

private native JavaScriptObject putString(JavaScriptObject jso, String key, String value)/*-{
    jso[key] = value;
    return jso;
}-*/;

private native JavaScriptObject putObject(JavaScriptObject jso, String key, JavaScriptObject value)/*-{
    jso[key] = value;
    return jso;
}-*/;

void someJavaFunction() {
    JavaScriptObject fromJava = JavaScriptObject.createObject();
    fromJava = putString(fromJava, "foo", "bar");
    fromJava = putObject(fromJava, "baz", fromJava);
}

Upvotes: 0

Andrey Mormysh
Andrey Mormysh

Reputation: 819

Variable $wnd.v will contain an array of objects. Note: you will need to find a way how to convert your Java objects to a JSON (i used restygwt).

class PersonList {
    List<Person> list;
}

class Person {
    String title;
    String firstName;
    String lastName;

    public Person () {}

    public Person(String title, String firstName, String lastName) {
        this.title = title;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}


public class Main implements EntryPoint {

    public interface PersonCodec extends JsonEncoderDecoder<PersonList> {
    }

    PersonCodec personCodec = GWT.create(PersonCodec.class);

    public void onModuleLoad() {
        List<Person> list = new ArrayList<Person>();

        list.add(new Person("Mr.", "Bill", "Gates"));
        list.add(new Person("Mr.", "Andrey", "Mormysh"));

        PersonList personList = new PersonList();
        personList.list = list;

        String json = personCodec.encode(personList).toString();

        setPersonList(json);
    }


    public static native void setPersonList(String personListJson)/*-{
        $wnd.v = eval("(" + personListJson + ")").list;
        alert($wnd.v[0].firstName); // Output: 'Bill'
    }-*/;
}

Upvotes: 0

mxro
mxro

Reputation: 6878

One approach could be to use a JSNI method to create the items/entries of your Array/Map as follows:

 JsArray arr = JavaScriptObject.createArray().cast();
 arr.push(newEntry("Mr.", "Bill", "Gates"));      

 ....

 private final native JavaScriptObject newEntry(String title, 
                               String firstname, String lastname)/*-{
     return {title: title, firstname: firstname, lastname: lastname};
 }-*/; 

You could also try to create the data structure you have in mind using the JSON utility methods: Put JSONObjects inside a JSONArray.

Upvotes: 4

Related Questions