T
T
Testtest1322014-09-18 20:16:27
backbone.js
Testtest132, 2014-09-18 20:16:27

What does this JS code do?

$service.Messages = new (Backbone.Collection.extend({
     model      : $service.Message,
     initialize : function() {
       var self = this, $el = $("#online"), $users = $("#users");
       socket.on("message:get", function (data) {
         self.add(data)
       });
       socket.on("user:added", function ( data, counter ) {
       var arr = [];
         $el.text( counter );
         $users.html("");
         for( var i = 0; i < data.length; i++ ) if( data[i] ) {
            arr.push("<b>" + data[i].username + "</b>");
          };
          $users.html( arr.join(", ") );
       });
     }
   }));

   $service.Messages.on("add", function( $item ) {
      var view = new $service.ViewMessage({model: $item});
      $( view.render().el ).prependTo(".message-append");
   });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2014-09-18
@AMar4enko

The service listens on a WebSocket and updates page elements when new messages arrive or a user is added.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question