github.com/nsqio/nsq@v1.3.0/nsqadmin/static/js/views/base.js (about) 1 var $ = require('jquery'); 2 var _ = require('underscore'); 3 var Backbone = require('backbone'); 4 5 var AppState = require('../app_state'); 6 7 var errorTemplate = require('./error.hbs'); 8 9 10 var BaseView = Backbone.View.extend({ 11 constructor: function(options) { 12 // As of 1.10, Backbone no longer automatically attaches options passed 13 // to the constructor as this.options, but that's often useful in some 14 // cases, like a className function, that happen before initialize() 15 // would have a chance to attach the same options. 16 this.options = options || {}; 17 return Backbone.View.prototype.constructor.apply(this, arguments); 18 }, 19 20 initialize: function() { 21 this.subviews = []; 22 this.rendered = false; 23 }, 24 25 template: function() {}, 26 27 skippedRender: function() {}, 28 29 render: function(data) { 30 if (this.renderOnce && this.rendered) { 31 this.skippedRender(); 32 return this; 33 } 34 this.removeSubviews(); 35 var ctx = this.getRenderCtx(data); 36 // console.log('render ctx: %o', ctx); 37 var html = this.template(ctx); 38 if (!this.removed) { 39 this.$el.empty(); 40 this.$el.append(html); 41 this.postRender(ctx); 42 } 43 this.rendered = true; 44 return this; 45 }, 46 47 getRenderCtx: function(data) { 48 var ctx = { 49 'graph_enabled': AppState.get('GRAPH_ENABLED'), 50 'graph_interval': AppState.get('graph_interval'), 51 'graph_active': AppState.get('GRAPH_ENABLED') && 52 AppState.get('graph_interval') !== 'off', 53 'nsqlookupd': AppState.get('NSQLOOKUPD'), 54 'version': AppState.get('VERSION') 55 }; 56 if (this.model) { 57 ctx = _.extend(ctx, this.model.toJSON()); 58 } else if (this.collection) { 59 ctx = _.extend(ctx, {'collection': this.collection.toJSON()}); 60 } 61 if (data) { 62 ctx = _.extend(ctx, data); 63 } 64 return ctx; 65 }, 66 67 postRender: function() {}, 68 69 appendSubview: function(subview, selector) { 70 return this.appendSubviews([subview], selector); 71 }, 72 73 appendSubviews: function(subviews, selector) { 74 this.subviews.push.apply(this.subviews, subviews); 75 var $el = selector ? this.$(selector) : this.$el; 76 $el.append(subviews.map(function(subview) { 77 return subview.render().delegateEvents().el; 78 })); 79 }, 80 81 removeSubviews: function() { 82 while (this.subviews.length) { 83 this.subviews.pop().remove(); 84 } 85 }, 86 87 remove: function() { 88 this.removed = true; 89 this.removeSubviews(); 90 Backbone.View.prototype.remove.apply(this, arguments); 91 }, 92 93 parseErrorMessage: function(jqXHR) { 94 var msg = 'ERROR: failed to connect to nsqadmin'; 95 if (jqXHR.readyState === 4) { 96 try { 97 var parsed = JSON.parse(jqXHR.responseText); 98 msg = parsed['message']; 99 } catch (err) { 100 msg = 'ERROR: failed to decode JSON - ' + err.message; 101 } 102 } 103 return msg; 104 }, 105 106 handleAJAXError: function(jqXHR) { 107 $('#warning, #error').hide(); 108 $('#error .alert').text(this.parseErrorMessage(jqXHR)); 109 $('#error').show(); 110 }, 111 112 handleViewError: function(jqXHR) { 113 this.removeSubviews(); 114 this.$el.html(errorTemplate({'message': this.parseErrorMessage(jqXHR)})); 115 } 116 }); 117 118 module.exports = BaseView;