Okay,
this was much simpler than expected.
SO ill share it :)

I need two divs: one called chat. The other one called ts. Its holding a
timestamp when the last update was made.


<script type="text/javascript">
//<![CDATA[
new PeriodicalExecuter(function() {
new Ajax.Request('/chat/get', 
{asynchronous:true, evalScripts:false, 
 onComplete:function(request, json){updateJSON(request, json)},
 parameters:'ts=' + $('ts').innerHTML})}, 10)
//]]>

var chat = $('chat');
function updateJSON(request, json)
{
  var nbElementsInResponse = json.length;
  for (var i = 0; i < nbElementsInResponse; i++){
     if (json[i][0] == 'c') {
       var doScroll = false;
       if(chat.scrollTop == (chat.scrollHeight - 200)){ doScroll = true}
       Element.update(chat, chat.innerHTML + json[i][1]);
       if(doScroll){ chat.scrollTop = chat.scrollHeight }
     } else {
      Element.update(json[i][0], json[i][1]);
     }
  }
}
//]]>
</script>

The get serverside action will check if there are new messages to deliver.
If that's the case its put into json. And ts is updated to a new timestamp.

To input data
<form onsubmit="new Ajax.Request('/chat/send', 
{asynchronous:true, evalScripts:false, 
onComplete:function(request, json){updateJSON(request,
json);$('message').value='';$('go').disabled=false},
onLoading:function(request, json){$('go').disabled=true},
parameters:Form.serialize(this)}); return false;" action="/chat/send"
method="post">

<input type="text" name="message" id="message" value="" />    
<input type="submit" name="commit" value="Go" id="go" />    
</form>


The send action on the server will just add the message into the database or
memory and then directly call the get action described above

Quite neat. And rapid to prototype. Now the finetuning will start. Comments
always welcome

.: Fabian 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to