topic.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var $ = require('jquery');
  2. window.jQuery = $;
  3. var bootstrap = require('bootstrap'); //eslint-disable-line no-unused-vars
  4. var bootbox = require('bootbox');
  5. var Pubsub = require('../lib/pubsub');
  6. var AppState = require('../app_state');
  7. var BaseView = require('./base');
  8. var TopicView = BaseView.extend({
  9. className: 'topic container-fluid',
  10. template: require('./spinner.hbs'),
  11. events: {
  12. 'click .topic-actions button': 'topicAction'
  13. },
  14. initialize: function() {
  15. BaseView.prototype.initialize.apply(this, arguments);
  16. this.listenTo(AppState, 'change:graph_interval', this.render);
  17. var isAdmin = this.model.get('isAdmin');
  18. this.model.fetch()
  19. .done(function(data) {
  20. this.template = require('./topic.hbs');
  21. this.render({'message': data['message'], 'isAdmin': isAdmin});
  22. }.bind(this))
  23. .fail(this.handleViewError.bind(this))
  24. .always(Pubsub.trigger.bind(Pubsub, 'view:ready'));
  25. },
  26. topicAction: function(e) {
  27. e.preventDefault();
  28. e.stopPropagation();
  29. var action = $(e.currentTarget).data('action');
  30. var txt = 'Are you sure you want to <strong>' +
  31. action + '</strong> <em>' + this.model.get('name') + '</em>?';
  32. bootbox.confirm(txt, function(result) {
  33. if (result !== true) {
  34. return;
  35. }
  36. if (action === 'delete') {
  37. $.ajax(this.model.url(), {'method': 'DELETE'})
  38. .done(function() { window.location = AppState.basePath('/'); });
  39. } else {
  40. $.post(this.model.url(), JSON.stringify({'action': action}))
  41. .done(function() { window.location.reload(true); })
  42. .fail(this.handleAJAXError.bind(this));
  43. }
  44. }.bind(this));
  45. }
  46. });
  47. module.exports = TopicView;