iegorov
10/18/2016 - 6:31 PM

Work with store and actions from component

Work with store and actions from component

(function() {
  'use strict';

  angular
    .module('direktkomplekt.app.profile')
    .component('dkGalleryAlbumList', {
    });

  GalleryAlbumList.$inject = [
    'fileAlbumsActions',
    'fileAlbumsStore'
  ];

  function GalleryAlbumList(
    fileAlbumsActions,
    fileAlbumsStore
  ) {
    var vm = this;
    
    var disposables = [];

    addDisposables();
    
    fileAlbumsActions.getFileAlbums();
    
    function addDisposables() {
      disposables.push(
        fileAlbumsStore.fileAlbums
          .subscribe(function(albums) {
            vm.albums = albums;
            vm.loadingProcess = false;
            vm.loadingError = false;
          }),
        fileAlbumsStore.error
          .subscribe(function() {
            vm.loadingErrorMessage = 'Ошибка получения галереи!';
            vm.loadingError = true;
            vm.loadingProcess = false;
          })
      );
    }
    this.$onDestroy = function() {
      disposables.forEach(function(disposable) {
        disposable.dispose();
      });
    };
  }
  
  // или так, если внутри не компонента
  // $scope.$on('$destroy', function() {
  //   disposables.forEach(function(disposable) {
  //     disposable.dispose();
  //   });
  // });

})();