diff --git a/rd_ui/app/scripts/controllers/dashboard.js b/rd_ui/app/scripts/controllers/dashboard.js index 39bef91e17..338d27bdb7 100644 --- a/rd_ui/app/scripts/controllers/dashboard.js +++ b/rd_ui/app/scripts/controllers/dashboard.js @@ -1,5 +1,5 @@ (function() { - var DashboardCtrl = function($scope, Events, Widget, $routeParams, $http, $timeout, Dashboard) { + var DashboardCtrl = function($scope, Events, Widget, $routeParams, $http, $timeout, $q, Dashboard) { $scope.refreshEnabled = false; $scope.refreshRate = 60; @@ -8,43 +8,49 @@ Events.record(currentUser, "view", "dashboard", dashboard.id); $scope.$parent.pageTitle = dashboard.name; - var filters = {}; + + var promises = []; $scope.dashboard.widgets = _.map($scope.dashboard.widgets, function (row) { return _.map(row, function (widget) { var w = new Widget(widget); if (w.visualization && dashboard.dashboard_filters_enabled) { - var queryFilters = w.getQuery().getQueryResult().getFilters(); - _.each(queryFilters, function (filter) { - if (!_.has(filters, filter.name)) { - // TODO: first object should be a copy, otherwise one of the chart filters behaves different than the others. - filters[filter.name] = filter; - filters[filter.name].originFilters = []; - - $scope.$watch(function () { - return filter.current - }, function (value) { - _.each(filter.originFilters, function (originFilter) { - originFilter.current = value; - }) - }); - - } - ; - - // TODO: merge values. - filters[filter.name].originFilters.push(filter); - }); + promises.push(w.getQuery().getQueryResultPromise()); } return w; }); }); - if (dashboard.dashboard_filters_enabled) { - $scope.filters = _.values(filters); - } + $q.all(promises).then(function(queryResults) { + var filters = {}; + _.each(queryResults, function(queryResult) { + var queryFilters = queryResult.getFilters(); + _.each(queryFilters, function (filter) { + if (!_.has(filters, filter.name)) { + // TODO: first object should be a copy, otherwise one of the chart filters behaves different than the others. + filters[filter.name] = filter; + filters[filter.name].originFilters = []; + + $scope.$watch(function () { return filter.current }, function (value) { + _.each(filter.originFilters, function (originFilter) { + originFilter.current = value; + }); + }); + }; + + // TODO: merge values. + filters[filter.name].originFilters.push(filter); + }); + }); + + if (dashboard.dashboard_filters_enabled) { + $scope.filters = _.values(filters); + } + }); + + }, function () { // error... // try again. we wrap loadDashboard with throttle so it doesn't happen too often.\ @@ -131,7 +137,7 @@ }; angular.module('redash.controllers') - .controller('DashboardCtrl', ['$scope', 'Events', 'Widget', '$routeParams', '$http', '$timeout', 'Dashboard', DashboardCtrl]) + .controller('DashboardCtrl', ['$scope', 'Events', 'Widget', '$routeParams', '$http', '$timeout', '$q', 'Dashboard', DashboardCtrl]) .controller('WidgetCtrl', ['$scope', 'Events', 'Query', WidgetCtrl]) })(); \ No newline at end of file diff --git a/rd_ui/app/scripts/services/resources.js b/rd_ui/app/scripts/services/resources.js index 983a0b8053..da9adc281f 100644 --- a/rd_ui/app/scripts/services/resources.js +++ b/rd_ui/app/scripts/services/resources.js @@ -1,5 +1,5 @@ (function () { - var QueryResult = function ($resource, $timeout) { + var QueryResult = function ($resource, $timeout, $q) { var QueryResultResource = $resource('/api/query_results/:id', {id: '@id'}, {'post': {'method': 'POST'}}); var Job = $resource('/api/jobs/:id', {id: '@id'}); @@ -32,6 +32,7 @@ } }); + this.deferred.resolve(this); } else if (this.job.status == 3) { this.status = "processing"; } else { @@ -40,6 +41,7 @@ } function QueryResult(props) { + this.deferred = $q.defer(); this.job = {}; this.query_result = {}; this.status = "waiting"; @@ -349,6 +351,10 @@ }); return queryResult; + }; + + QueryResult.prototype.toPromise = function() { + return this.deferred.promise; } QueryResult.get = function (data_source_id, query, ttl) { @@ -404,9 +410,15 @@ return this.queryResult; }; + Query.prototype.getQueryResultPromise = function() { + return this.getQueryResult().toPromise(); + } + return Query; }; + + var DataSource = function ($resource) { var DataSourceResource = $resource('/api/data_sources/:id', {id: '@id'}, {'get': {'method': 'GET', 'cache': true, 'isArray': true}}); @@ -435,7 +447,7 @@ } angular.module('redash.services') - .factory('QueryResult', ['$resource', '$timeout', QueryResult]) + .factory('QueryResult', ['$resource', '$timeout', '$q', QueryResult]) .factory('Query', ['$resource', 'QueryResult', 'DataSource', Query]) .factory('DataSource', ['$resource', DataSource]) .factory('Widget', ['$resource', 'Query', Widget]);