HighDots Forums  

Another AJAX question

Javascript JavaScript language (comp.lang.javascript)


Discuss Another AJAX question in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
slebetman
 
Posts: n/a

Default Re: Another AJAX question - 06-05-2008 , 09:31 PM






On Jun 5, 9:36 pm, sheldonlg <sheldonlg> wrote:
Quote:
Dan Rumney wrote:
sheldonlg wrote:
I am looking for a clean solution to a problem that I solved in, what
I call, a "dirty" way.

[snip]

Could you provide your dirty code, please?

Sure (in a mixture of code and pseudocode):

****************

Javascript processing:
(...the important two lines in handling the response...)
var resp = postProcess(pObjRequest.responseText);
document.getElementById('foo').innerHTML = resp;

function postProcess(txt) {
var hideIt = txt.substring(0,1);
var editButton = document.getElementById('showEdit');
if (hideIt == 0)
editButton.style.display = 'none';
else
editButton.style.display = '';
return txt.substring(1);

}

You can of course use closure to pass the required parameter:

var hideIt; // the variable you wish to pass
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (hideIt) {// here you can use the variable..}
}
}

If you want to pass the value rather than the variable itself:

var hideIt;
(function (myHideIt) {
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (myHideIt) {// check the passed value..}
}
}
})(hideIt) // pass the value of hideIt

Or if you prefer, you can wrap this up in a function that generates a
function:

function makeAjaxCallback (ajaxObject,myHideIt) {
return function () {
if (ajaxObject.readyState == 4) {
if (myHideIt) {// check the passed value..}
}
}
}
ajax.onreadystatechange = makeAjaxCallback(ajax,hideIt);

There are many ways to use closure to do this depending on exactly
what you want and, to some degree, preferred style.


Reply With Quote
  #12  
Old   
sheldonlg
 
Posts: n/a

Default Re: Another AJAX question - 06-06-2008 , 06:47 AM






slebetman wrote:
Quote:
On Jun 5, 9:36 pm, sheldonlg <sheldonlg> wrote:
Dan Rumney wrote:
sheldonlg wrote:
I am looking for a clean solution to a problem that I solved in, what
I call, a "dirty" way.
[snip]
Could you provide your dirty code, please?
Sure (in a mixture of code and pseudocode):

****************

Javascript processing:
(...the important two lines in handling the response...)
var resp = postProcess(pObjRequest.responseText);
document.getElementById('foo').innerHTML = resp;

function postProcess(txt) {
var hideIt = txt.substring(0,1);
var editButton = document.getElementById('showEdit');
if (hideIt == 0)
editButton.style.display = 'none';
else
editButton.style.display = '';
return txt.substring(1);

}


You can of course use closure to pass the required parameter:

var hideIt; // the variable you wish to pass
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (hideIt) {// here you can use the variable..}
}
}

If you want to pass the value rather than the variable itself:

var hideIt;
(function (myHideIt) {
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
if (myHideIt) {// check the passed value..}
}
}
})(hideIt) // pass the value of hideIt

Or if you prefer, you can wrap this up in a function that generates a
function:

function makeAjaxCallback (ajaxObject,myHideIt) {
return function () {
if (ajaxObject.readyState == 4) {
if (myHideIt) {// check the passed value..}
}
}
}
ajax.onreadystatechange = makeAjaxCallback(ajax,hideIt);

There are many ways to use closure to do this depending on exactly
what you want and, to some degree, preferred style.
I don't understand. How is the hideIt variable set (in the php
script)?. How does the javascript know that there is a variable hideIt
that is coming back? My line:

var resp = postProcess(pObjRequest.responseText);

is already inside an

if (ajaxObject.readyState == 4) {}

block that returns the string for the innerHTML.


Reply With Quote
  #13  
Old   
beegee
 
Posts: n/a

Default Re: Another AJAX question - 06-06-2008 , 08:49 AM



On Jun 6, 7:47*am, sheldonlg <sheldonlg> wrote:
Quote:
slebetman wrote:

I don't understand. *How is the hideIt variable set (in the php
script)?. *How does the javascript know that there is a variable hideIt
that is coming back? *My line:

var resp = postProcess(pObjRequest.responseText);

is already inside an

if (ajaxObject.readyState == 4) {}

block that returns the string for the innerHTML.
Okay. Asynchronous Javascript XML = AJAX. HTML is not a transport
protocol and generating HTML on the server through PHP responses sort
of contradicts the purpose of AJAX. Rethink your design. Use
Javascript to generate markup from data, use XML or JSON for
transporting that data. If you do this, some of the other poster's
solutions will begin to make sense.

I know there are tons of examples on the net of AJAH but, in my humble
opinion, that's why they're called script kiddies.

Bob


Reply With Quote
  #14  
Old   
sheldonlg
 
Posts: n/a

Default Re: Another AJAX question - 06-06-2008 , 09:21 AM



beegee wrote:
Quote:
On Jun 6, 7:47 am, sheldonlg <sheldonlg> wrote:
slebetman wrote:

I don't understand. How is the hideIt variable set (in the php
script)?. How does the javascript know that there is a variable hideIt
that is coming back? My line:

var resp = postProcess(pObjRequest.responseText);

is already inside an

if (ajaxObject.readyState == 4) {}

block that returns the string for the innerHTML.

Okay. Asynchronous Javascript XML = AJAX. HTML is not a transport
protocol and generating HTML on the server through PHP responses sort
of contradicts the purpose of AJAX. Rethink your design. Use
Javascript to generate markup from data, use XML or JSON for
transporting that data. If you do this, some of the other poster's
solutions will begin to make sense.

I know there are tons of examples on the net of AJAH but, in my humble
opinion, that's why they're called script kiddies.

Bob

I **think** I understand what you are saying.

This application happens to be for recipes, and the dropdown list is a
list of recipes. That dropdown list is not created through AJAX. Now,
when the user clicks on a particular recipe, I send an AJAX call. On
the server it looks up the data for the recipe from the database. What
I then do now is to generate the entire html portion of the recipe to
fit into a div on the bottom of the page (via innerHTML).

I guess what you are saying is to send back an xml string containing the
data and use javacript on the browser to process that xml string,
placing all the data into the appropriate tags on the bottom of the page
(since the bottom of the page always has the same fields). In that
case, including another field in the XML would, indeed, be trivial.

Am I reading you correctly?


Reply With Quote
  #15  
Old   
beegee
 
Posts: n/a

Default Re: Another AJAX question - 06-07-2008 , 09:22 AM



On Jun 6, 10:21 am, sheldonlg <sheldonlg> wrote:
Quote:
beegee wrote:
On Jun 6, 7:47 am, sheldonlg <sheldonlg> wrote:
slebetman wrote:

I don't understand. How is the hideIt variable set (in the php
script)?. How does the javascript know that there is a variable hideIt
that is coming back? My line:

var resp = postProcess(pObjRequest.responseText);

is already inside an

if (ajaxObject.readyState == 4) {}

block that returns the string for the innerHTML.

Okay. Asynchronous Javascript XML = AJAX. HTML is not a transport
protocol and generating HTML on the server through PHP responses sort
of contradicts the purpose of AJAX. Rethink your design. Use
Javascript to generate markup from data, use XML or JSON for
transporting that data. If you do this, some of the other poster's
solutions will begin to make sense.

I know there are tons of examples on the net of AJAH but, in my humble
opinion, that's why they're called script kiddies.

Bob

I **think** I understand what you are saying.

This application happens to be for recipes, and the dropdown list is a
list of recipes. That dropdown list is not created through AJAX. Now,
when the user clicks on a particular recipe, I send an AJAX call. On
the server it looks up the data for the recipe from the database. What
I then do now is to generate the entire html portion of the recipe to
fit into a div on the bottom of the page (via innerHTML).

I guess what you are saying is to send back an xml string containing the
data and use javacript on the browser to process that xml string,
placing all the data into the appropriate tags on the bottom of the page
(since the bottom of the page always has the same fields). In that
case, including another field in the XML would, indeed, be trivial.

Am I reading you correctly?

Yup. You have it. In this way you're emulating the classic client-
server application and keeping UI and data separate. If you don't
care about security too much, JSON is a even better bet than XML cause
there is no parsing involved on the Javascript side. You just eval
the result and you've got a javascript object.

Bob


Reply With Quote
  #16  
Old   
Jorge
 
Posts: n/a

Default Re: Another AJAX question - 06-07-2008 , 10:46 AM



On Jun 5, 3:36*pm, sheldonlg <sheldonlg> wrote:
Quote:
Javascript processing:
(...the important two lines in handling the response...)
var resp = postProcess(pObjRequest.responseText);
document.getElementById('foo').innerHTML = resp;

function postProcess(txt) {
* *var hideIt = txt.substring(0,1);
* *var editButton = document.getElementById('showEdit');
* *if (hideIt == 0)
* * *editButton.style.display = 'none';
* *else
* * *editButton.style.display = '';
* *return txt.substring(1);

}
What's wrong with that ?
I'd say that that's perfect.
Why don't you like it this way ?

But just for the sake of changing something :

function postProcess(txt) {
var s= document.getElementById('showEdit').style;
s.display = (txt.substring(0,1) === "0") ? 'none' : '';
return txt.substring(1);
}

--Jorge.


Reply With Quote
  #17  
Old   
sheldonlg
 
Posts: n/a

Default Re: Another AJAX question - 06-07-2008 , 01:31 PM



Jorge wrote:
Quote:
On Jun 5, 3:36 pm, sheldonlg <sheldonlg> wrote:
Javascript processing:
(...the important two lines in handling the response...)
var resp = postProcess(pObjRequest.responseText);
document.getElementById('foo').innerHTML = resp;

function postProcess(txt) {
var hideIt = txt.substring(0,1);
var editButton = document.getElementById('showEdit');
if (hideIt == 0)
editButton.style.display = 'none';
else
editButton.style.display = '';
return txt.substring(1);

}

What's wrong with that ?
I'd say that that's perfect.
Why don't you like it this way ?
I get an uncomfortable feeling when I do a quick-and-dirty like this,
just to get it tow work. I like the cleaner approach that was told to
me in another response in this thread. There, on the server, I would
prepare an XML statement to pass back all the data for the bottom of the
page. I would also add in a field, <showEdit> with the value of 0 or 1
inserted for the data. At the browser I would then process the XML to
get ALL the data, including this field, and do what was necessary for
all the fields.

I say "would", because I had already done what I showed up above. It
works and I am a firm believer in "if it ain't broke, don't fix it".
next time, though ....


Reply With Quote
  #18  
Old   
Jorge
 
Posts: n/a

Default Re: Another AJAX question - 06-07-2008 , 04:48 PM



On Jun 7, 8:31*pm, sheldonlg <sheldonlg> wrote:

Quote:
What's wrong with that ?
I'd say that that's perfect.
Why don't you like it this way ?

I get an uncomfortable feeling when I do a quick-and-dirty like this,
just to get it tow work. *I like the cleaner approach that was told to
me in another response in this thread. *There, on the server, I would
prepare an XML statement to pass back all the data for the bottom of the
page. *I would also add in a field, <showEdit> with the value of 0 or 1
inserted for the data. *At the browser I would then process the XML to
get ALL the data, including this field, and do what was necessary for
all the fields.
Rewrite it all, server and client-side, and send it as an XML doc just
because you need a boolean to enable/disable a single button ?
Hmm, you must be kidding, that's not a good idea, imho.

--Jorge.


Reply With Quote
  #19  
Old   
sheldonlg
 
Posts: n/a

Default Re: Another AJAX question - 06-07-2008 , 10:18 PM



Jorge wrote:
Quote:
On Jun 7, 8:31 pm, sheldonlg <sheldonlg> wrote:

What's wrong with that ?
I'd say that that's perfect.
Why don't you like it this way ?
I get an uncomfortable feeling when I do a quick-and-dirty like this,
just to get it tow work. I like the cleaner approach that was told to
me in another response in this thread. There, on the server, I would
prepare an XML statement to pass back all the data for the bottom of the
page. I would also add in a field, <showEdit> with the value of 0 or 1
inserted for the data. At the browser I would then process the XML to
get ALL the data, including this field, and do what was necessary for
all the fields.

Rewrite it all, server and client-side, and send it as an XML doc just
because you need a boolean to enable/disable a single button ?
Hmm, you must be kidding, that's not a good idea, imho.

--Jorge.

No, no. I am not rewriting anything. All I am saying is that a better
techniques is to separate the data from the presentation. That is why
to send the data via xml and let the client side handle the
presentation. That is even without the boolean. Adding the boolean is
simple if I had done it that way. The current code works and I am not
rewriting it. However, next time I will do it the XML way because that
leads to easier modifications and enhancements and does not require yet
another addition of yet another hack if something else comes along that
is required.


Reply With Quote
  #20  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: Another AJAX question - 06-08-2008 , 11:19 AM



In comp.lang.javascript message <b85ae10a-47d8-4e09-ab31-20c99ef24794@8g
2000hse.googlegroups.com>, Sat, 7 Jun 2008 08:46:54, Jorge
<jorge (AT) jorgechamorro (DOT) com> posted:

Quote:
But just for the sake of changing something :

function postProcess(txt) {
var s= document.getElementById('showEdit').style;
s.display = (txt.substring(0,1) === "0") ? 'none' : '';
return txt.substring(1);
}
Or
s.display = (txt.charAt(0) === "0") ? 'none' : ''; // untested

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.