I got a question when trying to wrap some Javascript APIs with JSNI of Google Web Toolkit. The javascript API is provided by Parse.com, a cloud based server. My code is like this:
public class MMParseComm {
private MMProject project = new MMProject();//MMProject has a member: String projectName.
private String _projectName;
private native void _retrieveCurrentProject_step1(int projectId) /*-{
var ProjectList = $wnd.Parse.Object.extend("ProjectList");
var query = new $wnd.Parse.Query(ProjectList);
query.equalTo("projectId", projectId);
query.find({
success: function(results) {
//Questions here: In this function, I can access "_projectName" by
//[email protected]::_projectName,
//but cannot access "project.projectName" using the same syntax.
//Also, I have searched around but cannot find a way
//that can write out a JSON data retrieved in "results"
//as a JSONObject in Java.
},
error: function(error) {
$wnd.alert("Error");
}
});
}-*/;
}
As shown on the code above, the questions are in the commented part of the code.
I'm not sure how can I write a string in array results
out into project.projectName
? Also, how can I wrap a JSON data retrieved from results
in javascript to be a JSONObject in Java?
Thanks in advance!
Weibin
I don't think the JSNI external call syntax supports chain invocation which is why you can't access project.projectName.
I assume you have a external JavaScript prototype of type Project. You would be better off here creating a overlay type than using the JSONObject API to parse it - it's more efficient and simpler to work with. I'll get you started..
public class MMProject extends JavaScriptObject
{
protected MMProject () { }
public final native String getProjectId() /*-{
return this.projectId;
}-*/;
}
Then utilize the wrapper with your success callback.
I suspect that results is probably an array type so your implementation might look like this:
public class MMParseComm
{
private JsArray<MMProject> projects = null;
private native void _retrieveCurrentProject_step1(int projectId) /*-{
var ProjectList = $wnd.Parse.Object.extend("ProjectList");
var query = new $wnd.Parse.Query(ProjectList);
query.equalTo("projectId", projectId);
query.find({
success: function(results) {
[email protected]::projects = results;
},
error: function(error) {
$wnd.alert("Error");
}
});
}-*/;
}
Without knowing the specifics of your object model this is the best I can advise you. If you need further clarity I can assist you.