(function (app, angular) { app .factory('$e5aMessage', function (noty, $rootScope) { if(!angular.isArray($rootScope.messages)) { $rootScope.messages = []; } var $e5aMessage = { subscribe: function ($scope, callback) { var handler = $rootScope.$on('$e5aMessage-event', callback); $scope.$on('$destroy', handler); }, success: function (message) { message = message || 'Zapisano zmiany'; var event = {text: message, type: 'success'}; noty.showSuccess(message, {callbacks: { onShow: function(){ event.active = true; }, onClose: function(){ event.active = false; } }}); $rootScope.$emit('$e5aMessage-event', event); }, failure: function (data) { console.error(data.data); var message = angular.isDefined(data.data.message) ? data.data.message : data.data; if ((typeof data.data) === "object" && !angular.isUndefined(data.data.code)) { if (data.data.code === '001') { message = 'Podana wartość [' + data.data.column + '] istnieje już w bazie. Podaj inną.'; } } noty.showError(message); $rootScope.$emit('$e5aMessage-event', {text: message, type: 'failure'}); } }; return $e5aMessage; }); })(app, angular);