I am using the direct web remoting library to do some ajax calls in my application. I am having a problem that I think comes down to a delayed response from the service call. Below is part of my code that I think is having the problem. The problem is in getDefaultReviewerTypeCode, the return variable isn't set in the call back till after other "stuff" processes. Is there a way to tell DWR to wait for a response before it continues processing the java script?
function makeProtocolReviewerTypesDropDown(reviewerTypes, reviewerIndex) {
var defaultReviewerType = getDefaultReviewerTypeCode();
...
var option = document.createElement('option');
option.setAttribute("value", "");
if (defaultReviewerType == '') {
option.setAttribute("selected", "selected");
}
...
for (var i = 0; i < reviewerTypes.length; i += 2) {
var reviewerType = reviewerTypes[i].replace(/^\t*/, '');
option = document.createElement('option');
option.setAttribute("value", reviewerType);
if (defaultReviewerType == reviewerType) {
option.setAttribute("selected", "selected");
}
option.text = reviewerTypes[i+1];
addSelectOption(selectElement, option);
}
return selectElement;
}
function getDefaultReviewerTypeCode() {
var defaultReviewTyper;
var dwrReply = {
callback:function(data) {
if ( data != null ) {
defaultReviewTyper = data;
} else {
defaultReviewTyper = '';
}
}
};
IacucProtocolActionAjaxService.getDefaultCommitteeReviewTypeCode(dwrReply);
return defaultReviewTyper;
}
The best way is to encapsulate all code that follows the DWR call into a separate method, and call that from the DWR callback. That way, your code that depends on the DWR results is guaranteed to be called only after DWR returns.
Related
Actually I've been reading about this for a while but I couldn't understand it very well.
Here is a snippet of the Servlet "ProcessNurseApp" :
if (dbm.CheckExRegNumber(Candidate.getRegNumber()) == true) {
// Show him an alert and stop him from applying.
out.println("<script>\n"
+ " alert('You already Applied');\n"
+ "</script>");
out.println("<script>\n"
+ " window.history.go(-1);\n"
+ "</script>");
}
So when the form named "ApplicationForm" in the "Nurses.jsp" get submitted it goes to that method in servlet after some Javascript validation.
My issue is that I want to call that method
if (dbm.CheckExRegNumber(Candidate.getRegNumber()) == true)
in the JSP page without getting to servlet so I can update values without refreshing the page. I've been reading that using ajax with jQuery would be the best way to do that, so can anyone help me of calling the above if statement from jQuery by AJAX.
Try an ajax call to the servlet(not possible without calling servlet) to check whether the function returns true or false then return a flag according to the value(true or false). On that basis you can show an alert or anything else.
For ajax call, you can use:
$.post( "ajax/Servlet_Url", function( data ) { if(data==true) alert("You already Applied"); else window.history.go(-1);});
Refer to following Link for more details about jQuery post request.
https://api.jquery.com/jquery.post/
jQuery(document).ready(function($) {
$("#searchUserId").attr("placeholder", "Max 15 Chars");
$("#searchUserName").attr("placeholder", "Max 100 Chars");
$.ajax({
url:"../../jsp/user/userMaster.do",
data: { drpType : 'userType',lookType : "1" },
success: function (responseJson) {
var myvalue = document.getElementById("userTypeKey");
for(var val in responseJson)
{
valueType = val;
textOptions = responseJson[val];
var option = document.createElement("option");
option.setAttribute("value",valueType);
option.text = textOptions;
myvalue.add(option);
if(valueType == myvalue.value)
{
option.selected = "selected";
}
}
}
});
});
I am trying to implement this code using java and the cloud code option from Parse.
Does anyone know how to implement this code in Java and then correctly call it from my function using PFCloud?
//Search for submissions & groups
var findUsersInGroupData:PFQuery = PFQuery(className: "GameInfo")
findUsersInGroupData.whereKey("Group", equalTo:groupName)
findUsersInGroupData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!)->Void in
if error == nil{
for object in objects{
let usersInGroup:PFObject = object as PFObject
self.timelineData.addObject(usersInGroup)
var findTimelineData:PFQuery = PFQuery(className: "Submissions")
findTimelineData.whereKey("User", equalTo: usersInGroup.objectForKey("User"))
findTimelineData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!)->Void in
if error == nil{
for object in objects{
let submission:PFObject = object as PFObject
self.timelineData.addObject(submission)
}
let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects
self.timelineData = NSMutableArray(array: array)
self.tableView.reloadData()
}
}
}
}
}
i am playing around a litle with riak an the riak-java-client.
Now i run into trouble with custom javascript, i want to use in a map reduce query.
If i use the pure javascript functions as anon functions, they work well.
So here is what i did:
uncommented in app.conf
{js_source_dir, "/tmp/js_source"},
then i stored mylib.js in /tmp/js_source
/* content of mylib.js */
var NS = (function() {
return {
mapHighValues: function(value, keydata, arg) {
var data = JSON.parse(value.values[0].data);
ejsLog('/tmp/map_reduce.log', JSON.stringify(data.High));
return [data.High];}
},
reduceSumHighValues: function(values) {
ejsLog('/tmp/map_reduce.log', "ReduceVals\n" + JSON.stringify(values));
return [values.reduce(function(prev, curr, index, array) {return prev + curr} ,0)];
}
}
})();
after that i restarted riak.
Here is the relevant java code:
MapReduceBuilder builder = new MapReduceBuilder(new RiakClient("localhost"))
.setBucket("goog")
.map(JavascriptFunction.named("NS.mapHighValues"), false)
.reduce(JavascriptFunction.named("NS.reduceSumHighValues"), true);
MapReduceResponseSource response = builder.submit();
Does anyone see my mistake?
Cheers
ApeHanger
Looks like an extra '}' after 'return [data.High];'
On the server side, I have a list into a bean.
On the Client side, I use:
function callJava() {
$.getJSON("../reference/test", { name: $('#name').val()}, function(result) {
// result is a bean that has a list
alert(result.fooList.length);
});
}
I need to be able to render this list later via FreeMarker.
What is killing me when I replaced this list with a String variable, it works fine like:
function callJava() {
$.getJSON("../reference/test", { name: $('#name').val()}, function(result) {
alert(result.stringVariable)
});
}
How could I deal with the string into that bean !!
If all you want to do is manipulate the elements of the list:
function callJava() {
$.getJSON("../reference/test", { name: $('#name').val()}, function(result) {
for (var i = 0; i < result.fooList.length; ++i)
alert(result.fooList[i]);
});
}
Actually I have just an update over my question is that the list I'm trying to return from the server side is a SCALA list . I have solved this issue by using an array instead of JAVA . and it works fine by using the following as Pointy said :
function callJava() {
$.getJSON("../reference/test", { name: $('#name').val()}, function(result) {
for (var i = 0; i < result.fooList.length; ++i)
alert(result.fooList[i]);
});
}
Could you capture the JSON response and post it? The jQuery getJSON method will silently swallow any parsing errors from malformed JSON. That is probably what happened.
I have a spring controller with a request mapping as follows
#RequestMapping("/downloadSelected")
public void downloadSelected(#RequestParam String[] ids) {
// retrieve the file and write it to the http response outputstream
}
I have an html table of objects which for every row has a checkbox with the id of the object as the value. When they submit, I have a jQuery callback to serialize all ids. I want to stick those ids into an http request parameter called, "ids" so that I can grab them easily.
I figured I could do the following
var ids = $("#downloadall").serializeArray();
Then I would need to take each of the ids and add them to a request param called ids. But is there a "standard" way to do this? Like using jQuery?
I don't know about "standard way", but this is how I would do it.
var ids = $("#downloadall").serializeArray();
will give you a dataset on the form (only the checked items presented):
[{name:"foo1", value:"bar1"}, {name:"foo2", value:"bar2"}]
To feed this to jQuery's .ajax() just:
$.ajax({
url: <your url>,
data: ids.map(function (i) {return i.name+'='+i.value;}).join('&')
});
The Array.map() is not compatible with all browsers yet so you better have this code on your page too:
if (!Array.prototype.map) {
Array.prototype.map = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
This code snippet I got from mozilla developer center.
I didn't put them in a ?ids=... param, but this way they are easy to access on server side. You can always just modify the map function to fit your needs.