/ Published in: HTML
This source code based on the "friend" example given with Persevere. It is a simple Twitter-like microblogging program. Note that you need to create a class definition file in "WEB-INF\jslib\tweet.js" for it to work.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <!-- Following Tweet class definition must exist in \WEB-INF\jslib\tweet.js: Class( { "id":"Tweet", "name":"Tweet", "schema":{ "extends":{"$ref":"../Class/Object"}, "prototype":{}, "properties":{ "name": { optional:true }, "content": { optional:true } } } }); --> <script type="text/javascript"> onload = function() { // load all the classes from Persevere displayTweets = function() { pjs.load("/Tweet/", function(tweets) { var tableHTML = ""; for(var i = tweets.length -1 ; i >= 0; i--) { var tweet = tweets[i]; var escapedName = tweet.name.replace(/</,''); var escapedContent = tweet.content.replace(/</,''); var age = ((new Date().getTime() - tweet.timestamp) / 1000) + " seconds ago"; tableHTML += "<p>" + escapedName + " : " + escapedContent + " (" + age + ")</p>"; } document.getElementById("list").innerHTML = tableHTML; }); } pjs.loadClasses(/*path*/ null, displayTweets); addTweet_onclic = /*document.getElementById("addTweet").onclick =*/ function() { var name = document.getElementById("sender").value; var message = document.getElementById("message").value; if(typeof Tweet == 'undefined'){ return alert('A "Tweet" class must be created in order for this demo to work'); } if(name && message){ new Tweet({"name":name,"content":message,"timestamp":new Date().getTime()}); // WHY? "message" field (instead of "content") does not work pjs.commit(displayTweets); } }; } </script> </head> <body> <table border='0' margin='5px'> </table> <div id="list" border="1"> </div> </body> </html>