app_srv.ts 939 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import angular from 'angular';
  4. export class AppSrv {
  5. apps: any = {};
  6. /** @ngInject */
  7. constructor(
  8. private $rootScope,
  9. private $timeout,
  10. private $q,
  11. private backendSrv) {
  12. }
  13. get(type) {
  14. if (this.apps[type]) {
  15. return this.$q.when(this.apps[type]);
  16. }
  17. return this.getAll().then(() => {
  18. return this.apps[type];
  19. });
  20. }
  21. getAll() {
  22. if (!_.isEmpty(this.apps)) {
  23. return this.$q.when(this.apps);
  24. }
  25. return this.backendSrv.get('api/org/apps').then(results => {
  26. return results.reduce((prev, current) => {
  27. prev[current.type] = current;
  28. return prev;
  29. }, this.apps);
  30. });
  31. }
  32. update(app) {
  33. return this.backendSrv.post('api/org/apps', app).then(resp => {
  34. this.apps[app.type] = app;
  35. });
  36. }
  37. }
  38. angular.module('grafana.services').service('appSrv', AppSrv);