this.recline = this.recline || {}; recline.DataExplorer = Backbone.View.extend({ tagName: 'div', className: 'data-explorer', template: ' \
\ ', events: { 'change input[name="nav-toggle"]': 'navChange' }, initialize: function() { this.el = $(this.el); this.render(); var self = this; // retrieve basic data like headers etc // note this.model and dataset returned are the same this.model.fetch().then(function(dataset) { // initialize of dataTable calls render self.dataTable = new recline.DataTable({ model: dataset }); self.flotGraph = new recline.FlotGraph({ model: dataset }); self.flotGraph.el.hide(); self.el.append(self.dataTable.el) self.el.append(self.flotGraph.el); }); }, render: function() { $(this.el).html($(this.template)); }, navChange: function(e) { // TODO: really ugly and will not scale to more widgets ... var widgetToShow = $(e.target).val(); if (widgetToShow == 'datatable') { this.flotGraph.el.hide(); this.dataTable.el.show(); } else if (widgetToShow == 'graph') { this.flotGraph.el.show(); this.dataTable.el.hide(); // Have to call this here // If you attempt to render with flot when graph is hidden / invisible flot will complain with // Invalid dimensions for plot, width = 0, height = 0 // (Could hack this by moving plot left -1000 or similar ...) this.flotGraph.createPlot(); } } }); recline.DataTable = Backbone.View.extend({ tagName: "div", className: "data-table-container", // template: TODO ??? events: { }, initialize: function() { this.el = $(this.el); var that = this; this.model.getRows().then(function(rows) { that._currentRows = rows; that.render() }); }, toTemplateJSON: function() { var modelData = this.model.toJSON() modelData.rows = _.map(this._currentRows, function(row) { var cellData = _.map(modelData.headers, function(header) { return {header: header, value: row[header]} }) return { id: 'xxx', cells: cellData } }) modelData.notEmpty = ( modelData.headers.length > 0 ) return modelData; }, render: function() { var template = $( ".dataTableTemplate:first" ).html() , htmls = $.mustache(template, this.toTemplateJSON()) ; $(this.el).html(htmls); return this; } }); recline.FlotGraph = Backbone.View.extend({ tagName: "div", className: "data-graph-container", // TODO: normalize css template: ' \ \