PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/chatter4/chatter4/static/chatter.js

https://bitbucket.org/hhru/gevent-socketio
JavaScript | 87 lines | 59 code | 23 blank | 5 comment | 0 complexity | 7d8cb8dc608712067fefddb122b03712 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. $(document).ready(function() {
  2. // connect to the websocket
  3. var socket = io.connect();
  4. var ChatModel = Backbone.Model.extend({
  5. });
  6. var ChatItem = Backbone.View.extend({
  7. render: function(){
  8. var template = Handlebars.compile($("#chat_item_template").html());
  9. this.$el.html(template(this.model));
  10. return this;
  11. },
  12. });
  13. // a collection that will hit a RESTful webservice and pull back all
  14. // results serialized as backbone models
  15. var ChatCollection = Backbone.Collection.extend({
  16. url: "/get_log"
  17. });
  18. var ChatView = Backbone.View.extend({
  19. events: {
  20. "submit #chat_form": "send"
  21. },
  22. send: function(evt) {
  23. evt.preventDefault();
  24. var val = $("#chatbox").val();
  25. socket.emit("chat", val);
  26. $("#chatbox").val("");
  27. },
  28. initialize: function() {
  29. var me = this;
  30. socket.on("chat", function(e) {
  31. me.$("#chatlog").append(new ChatItem({
  32. model: e
  33. }).render().el);
  34. });
  35. // re-render the view if the collection fires a reset event
  36. this.collection.on("reset", function() {
  37. me.render();
  38. });
  39. },
  40. render: function(){
  41. var template = Handlebars.compile($("#chat_template").html());
  42. $(this.el).html(template({
  43. collection: this.collection.toJSON()
  44. }));
  45. },
  46. });
  47. var Router = Backbone.Router.extend({
  48. routes: {
  49. "": "index"
  50. },
  51. index: function() {
  52. var collection = new ChatCollection();
  53. var view = new ChatView({
  54. collection: collection,
  55. el: $("#container")
  56. });
  57. // pull the data from the server
  58. collection.fetch();
  59. view.render();
  60. }
  61. });
  62. var router = new Router();
  63. Backbone.history.start({ pushState: true });
  64. });