Hi Marc,
On 21 May 2010 17:27, Marc Delisle marc@infomarc.info wrote:
Ninad Pundalik a écrit :
Hi All,
The start of the GSoC coding period is just around the corner, and I would like to start writing some useful code as soon as possible. I have a few questions before I start doing that though. In my proposal, I have mentioned one of my tasks as writing functions that will wrap the output of AJAX requests in well-formed XML. Over the last few days, I have been trying to figure out how to do this. Should I use output buffering at the start of an ajax request, read the entire output buffer into a string variable and then construct either a DOM or a SimpleXML object from that string when the request ends? Or, is there a better way of doing this?
Hi Ninad,
In your deliverables you wrote: "2. Functions that will wrap the output in well formed XML, so that jQuery can correctly parse the response and insert the right content at the right location in the page."
I guess that this would not be necessary if you use JSON?
If we use JSON, we would still need equivalent functions for wrapping the plain HTML (and JavaScript) generated on the fly by the PHP scripts. The use case which I had in mind for the above functions is the following:
1. Script a.php has sections of code foo, bar, bar1, baz and baz1 which are executed in the same sequence when a normal HTTP request is made for that page. 2. When an Ajax request is made for a.php, however, only sections foo, bar1 and baz1 are to be executed as bar and baz generate navigation links (or some other inconsequential division on the page) which we do not need in an Ajax request. 3. Hence, I can put a check for $_REQUEST['ajax_request'] and conditionally execute only foo, bar1 and baz1 if it's an Ajax request. Now, the output generated would be a series of HTML divisions, with unique ids. 4. If the page has been constructed in such a way that the output generated by foo, bar1 and baz1 are not continuous, the output generated in the previous step must have each of the divisions separate, so that I can use jQuery and place the HTML of each one at the correct location on the page.
If we use plain HTML, jQuery cannot traverse the data it receives from the Ajax call and insert different divisions at different places. Initially, I wanted to wrap the output by foo, bar1 and baz1 in XML nodes or as elements of a JSON array. However, I've just realized, that I can spare myself all this trouble and insert all the data that is sent as a response in a hidden temporary division on the page and then use jQuery to insert each section at the proper place.
The only concern that I have with this approach is that the JavaScript/jQuery code will be tightly coupled with the page for which it is written. A possible work around to this concern is to use jQuery's iterative nature and 'register' the different elements where the content will be replaced, and use a generic callback function at the end of the Ajax call to do the hard work.
As for simple actions (like deleting an entry from a table or reloading privileges), I will use JSON to generate success/error messages and not XML.
Please do not use wrappers that do nothing; as a bad example in PHP:
Understood. :)
I suggest to put them under /js.
This directory already has .js files for some pages (eg.: server_privileges.js). Where such files do not exist, I will create a .js file in this directory with the appropriate name.
Ninad S. Pundalik Twitter: @ni_nad | Identica : @ninad | http://ninadpundalik.co.cc/blog GPG Key Fingerprint: 2DF7 B856 C75E C9F9 0504 C0EF D456 1946 7C45 2C69