process further

0 views
Skip to first unread message

chrysanthem

unread,
Jul 8, 2009, 7:17:05 AM7/8/09
to Prototype & script.aculo.us
Hello
On return from a ajax call with prototype, is it possible to send the
return parameters to a function to be processed first and then write
that return value to the designated element? Also where is the syntax
on how to write the return values? I am having difficulty
understanding what I can use as text and what I can use as a
variable. tia.

David Behler

unread,
Jul 8, 2009, 12:25:19 PM7/8/09
to Prototype & script.aculo.us
I guess you would have to use Ajax.Request to process the returned
value before updating the designated element.

But I'm beginner and could be wrong here.

chrysanthe m

unread,
Jul 8, 2009, 10:37:55 PM7/8/09
to prototype-s...@googlegroups.com
Hi David
Thanks, but I think I one step further.  I am assuming an Ajax.request() has happened, returned and what I am looking at to understand further is syntax like this:
$('fullName').value = json.fullName;
for an returned parameter fullName and an html element ID of fullName. I am wondering, can I do
$('fullName').value =processMe( json.fullName);
which I know the obvious is try it.  I will, just trying to get an heads up.  I will report back.  Also I am assuming that prototpye is processing that nomenclature $('fullName') segment and just doing a document.getElementById.  Any insight appreciated.

Douglas

unread,
Jul 9, 2009, 12:38:07 AM7/9/09
to prototype-s...@googlegroups.com
Hello,
all you have to do (I guess) is set a callback to onComplete.
Something as follow:

// onComplete callback's call
function processJsonPart (myCoolVar)
{
// do something with your returned JSON
return myCoolVarEvaluatedOrValidatedOrAnythingYouWant;
}

// here we call Ajax.request
function clickButtonExample()
{
new Ajax.Request(url, {
onComplete: function(r)
{
var json = r.responseText.evalJSON(true);
$('name').value = foo(json.name);
}
}
}

PS: untested functions.
--
Believe nothing, no matter where you read it, or who said it, no
matter if I have said it, unless it agrees with your own reason and
your own common sense.

Douglas

unread,
Jul 9, 2009, 12:39:32 AM7/9/09
to prototype-s...@googlegroups.com
oops!

'foo' should be 'processJsonPart'

sathvik

unread,
Jul 9, 2009, 12:24:54 AM7/9/09
to prototype-s...@googlegroups.com
Hi,
I am a newbie too , but I think if processMe() is method of a class , u can call that method using 'this' like
$('fullname').value = this.processMe(parameter);

T.J. Crowder

unread,
Jul 9, 2009, 8:41:05 AM7/9/09
to Prototype & script.aculo.us
Hi,

> ...like this:
> $('fullName').value = json.fullName;
> for an returned parameter fullName and an html element ID of fullName.

It would be helpful if you gave a more thorough explanation of what
you're looking to do. But, just for the sake of an example, let's
assume:

1. You are sending back properly-formatted JSON with the correct MIME
type in response to an Ajax.Request.

2. The JSON data will have a success flag and firstName and lastName
properties, like this:

{
'success': true,
'firstName': 'Joe',
'lastName': 'Bloggs'
}

3. You want to display the names in the format "firstName lastName" in
an element that already exists on the page with the ID "fullName".

Here's how you can do that:

function showError(errmsg) {
alert(errmsg); // or whatever
}

function showName(elm, obj) {
elm.update(json.firstName + " " + json.lastName);
}

new Ajax.Request(url, {
// Success handler
onSuccess: function(response) {
var json, elm;

// Get the object containing the data from the request.
// Since you returned it with the correct content type
(application/json),
// Prototype has _already_ deserialized it for you and put it
in the
// member 'responseJSON'. (You don't need to use evalJSON()
unless
// you're not setting the content type correctly.)
json = response.responseJSON;

// Check that the JSON was returned
if (!json || !json.success) {
showError("Couldn't get the name.");
} else {
// Get the "fullName" element
elm = $('fullName');
if (!elm) {
// Show an error? The element is missing.
} else {
// Set its content from the JSON data
showName(elm, json);
}
}
},

// Failure handler
onFailure: function(response) {
showError("Error getting the name from the server.");
}
});

Hopefully that'll get you headed the right way. Although it doesn't
talk about JSON, this article[1] on the unofficial wiki may also be
helpful in terms of bulletproofing your Ajax requests.

[1] http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-requests

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available
Reply all
Reply to author
Forward
0 new messages