PDA

View Full Version : What is the prefered way to deal with arrays in JsObject?



Mike Chaliy
28 May 2009, 4:44 AM
Hi, I have an object that looks like



{
total: 50,
rows: [
{t1: 't1', t2: 't2'},
{t1: 't3', t2: 't4'}
]
}


Now I want to use it



JsObject o = new JsObject(source);
int total = o.getInt("total");
List rows = o.get ... // Here is somethign that I ask


I have ended with the following:



public class JsObjectEx extends JsObject {
public JsObjectEx() {
super();
}
public JsObjectEx(String data) {
super(data);
}
private JsObjectEx(JavaScriptObject jsObject) {
if (jsObject != null){
this.jsObject = jsObject;
} else {
this.jsObject = JsUtil.eval("[{}]");
}
}

public List<JsObjectEx> getList(String key){
final JsArray<JavaScriptObject> nativeList = getNativeList(key);
final List<JsObjectEx> list = new ArrayList<JsObjectEx>(nativeList.length());
for(int i = 0; i < nativeList.length(); i++){
list.add(new JsObjectEx(nativeList.get(i)));
}
return list;
}
private native JsArray<JavaScriptObject> getNativeList(String name) /*-{
var js = this.@com.extjs.gxt.ui.client.js.JsObject::jsObject;
return js[name];
}-*/;
}


But I am not sure that I am not inventing wheels here...

Please let me know your thoughts...