channel.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var _ = require('underscore');
  2. var AppState = require('../app_state');
  3. var Backbone = require('backbone');
  4. var Channel = Backbone.Model.extend({
  5. idAttribute: 'name',
  6. constructor: function Channel() {
  7. Backbone.Model.prototype.constructor.apply(this, arguments);
  8. },
  9. url: function() {
  10. return AppState.apiPath('/topics/' +
  11. encodeURIComponent(this.get('topic')) + '/' +
  12. encodeURIComponent(this.get('name')));
  13. },
  14. parse: function(response) {
  15. response['nodes'] = _.map(response['nodes'] || [], function(node) {
  16. var nodeParts = node['node'].split(':');
  17. var port = nodeParts.pop();
  18. var address = nodeParts.join(':');
  19. var hostname = node['hostname'];
  20. node['show_broadcast_address'] = hostname.toLowerCase() !== address.toLowerCase();
  21. node['hostname_port'] = hostname + ':' + port;
  22. return node;
  23. });
  24. response['clients'] = _.map(response['clients'] || [], function(client) {
  25. var clientId = client['client_id'];
  26. var hostname = client['hostname'];
  27. var shortHostname = hostname.split('.')[0];
  28. // ignore client_id if it's duplicative
  29. client['show_client_id'] = (clientId.toLowerCase() !== shortHostname.toLowerCase()
  30. && clientId.toLowerCase() !== hostname.toLowerCase());
  31. var port = client['remote_address'].split(':').pop();
  32. client['hostname_port'] = hostname + ':' + port;
  33. return client;
  34. });
  35. return response;
  36. }
  37. });
  38. module.exports = Channel;