<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
One important distinction between Prototype and some other frameworks
is that Prototype really focuses on basic functionality. It doesn''t
include widgets, just wrappers and utility classes so there isn''t much
to demo. It''s all about using various parts of prototype along with
other generic javascripting to create your own widgets and
functionality. Also, it is server-agnostic so giving server-side code
demos wouldn''t make much sense, but these things would be nice from the
respective communities of each server-side platform.<br>
<br>
That said, I''ll share with you my PHP-Ajax philosophies since I think
they may be useful. I don''t use a standard templating library like I
probably should, but what I''ve done has worked well so far.<br>
<br>
I use a separate "data" page for each Ajax-heavy page I serve. So for
example if I have useradmin.php, all of the ajax requests for that page
are handled by useradmindata.php<br>
I use the word data because this is what is served by the page, raw
(JSON) data. Sometimes I''ll serve up HTML as well, but I hate
generating PHP for server requests.<br>
<br>
Here is what useradmindata.php might look like:<br>
<?<br>
//do authentication checks and includes here<br>
<br>
//declare some commonly used variables<br>
$json = Array();<br>
$error = false;<br>
$printIsError = true;<br>
<br>
ob_start();<br>
$action = getPostOrGetValue(''action'');<br>
$value = trim(getPostOrGetValue(''value''));<br>
$id = dbEscapeString(getPostOrGetValue(''id''));<br>
$ids = parseid($id); // parses ''user-5_item-83'' into hash:
{user:5,item:83}<br>
<br>
switch( $action ){<br>
case ''doSomething'':<br>
if($value ==
''notCool'') $error = ''You did something not
cool.'';<br>
else
$json[''userInfo''] = Array(''id'' => 10,
''name'' =>
''Joe'', ''data'' =>
Array(5,6,7,8));<br>
$json[''status''] = getStatus($value);<br>
break;<br>
...<br>
}<br>
<br>
$content = ob_get_clean();<br>
if($printIsError && $content){<br>
$error .=
htmlspecialchars(strip_tags($content),ENT_QUOTES);<br>
$content = '''';<br>
}<br>
if($error){<br>
$json[''error''] =
$error;<br>
}<br>
if(!empty($json))<br>
header(''X-JSON:
(''.json_encode($json).'')'');<br>
if($content) print $content;<br>
?><br>
<br>
The javascript may look something like this:<br>
-------------------<br>
new Ajax.Request(''useradmindata.php'',{<br>
parameters: {<br>
action:
''doSomething'',<br>
value:
''cool''<br>
},<br>
onSuccess: function(xhr,json){<br>
if(json &&
json.error){ alert(json.error); return; }<br>
$(json.userInfo.id).update(json.userInfo.name);<br>
json.userInfo.data.each(function(value){<br>
$(''checkbox-''+value).checked =
''checked'';<br>
});<br>
$(''userStatus'').addClassName(json.status);<br>
}<br>
});<br>
--------------------<br>
<br>
So I always use action=somethingDescriptive in my Ajax requests, and
inside the switch statement I have code that handles that action. If a
JSON response is appropriate, it fills the $json variable with that
data, which will then be directly accessible in the client code inside
an onSomething callback. For cases where the response data may be large
I use print and set $printIsError = false, then in the client code I
have to xhr.responseText.evalJSON() to get the data. What the data is
or how you use it is entirely up to you. It could be an array or hash
of data that you can use with Builder.node, or messages or status flags
or whatever, be creative.<br>
<br>
There are many ways to structure your code, Rails and other MVCs seem
to like generating chunks of HTML/Javscript as the response, but if you
are doing this manually it gets pretty messy. Separating the initial
page load (HTML/Javascript), the Ajax requests (typically just POST/GET
encoded data), the Ajax response (JSON data is my favorite), and
client-side processing (Generating DOM nodes, JS widgets, settings
classNames, alerting error messages, etc..) is a good approach IMO.
Having JS that generates DOM elements can save on extra Ajax requests
and benefits from caching. Responding with just data keeps your code
well organized and keeps UI and backend data handling logically
distinct. Also, debugging blocks of code that are generated by the
server and evaled by the client can be a major PITA, having all of your
JS in .js files makes debugging a breeze.<br>
<br>
Does this help?<br>
<br>
Colin<br>
<br>
<br>
wyo wrote:
<blockquote
cite="mid:1175843735.815732.56980-8ox99JRHX6eLxTqHJMs4EmB/v6IoIuQBVpNB7YpNyf8@public.gmane.org"
type="cite">
<pre wrap="">On Apr 5, 11:11 pm, "Justin Perkins"
<a class="moz-txt-link-rfc2396E"
href="mailto:justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"><justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org></a>
wrote:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">Could anybody point me to prototype demos and
sample code?
</pre>
</blockquote>
<pre wrap="">Though there''s not really demos, did
you see this page:<a class="moz-txt-link-freetext"
href="http://prototypejs.org/learn/introduction-to-ajax">http://prototypejs.org/learn/introduction-to-ajax</a>
</pre>
</blockquote>
<pre wrap=""><!---->Yes I''ve read all three
tutorials but are most interested in JSON/PHP.
</pre>
<blockquote type="cite">
<pre wrap="">Ajax requires server-side code, which makes it
hard to demo something
in your language of choice, so that could be why it was left off of
the prototype website.
</pre>
</blockquote>
<pre wrap=""><!---->True, yet Prototype probably looses
every second visitor just because
of no demos. I know you can''t put up demos for all server sides but at
least one or some for the most used would help a lot. Another maybe
even better way is to put up downloadable samples so anybody can get
his favorite sample an try it out in his own environment.
Another very important aspect of working samples, they''re already
working! That way one can tweek them until they suit one''s own needs.
In case of errors one can always fall back to the working code and
start afresh.
O. Wyss
</pre>
</blockquote>
<br>
--~--~---------~--~----~------------~-------~--~----~<br>
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group. <br> To post to this
group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> To
unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
<br> For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en <br>
-~----------~----~----~----~------~----~------~--~---<br>
</body>
</html>
<br>