|
9 | 9 | 'use strict';
|
10 | 10 |
|
11 | 11 | angular.module('ngAngularError', ['ng'])
|
12 |
| - .factory('errorsList', function($http) { |
| 12 | + .factory('list', function($http) { |
13 | 13 | /* errors_list.json should contain a map of errors and user-feedback.
|
14 | 14 | * we use, $http to retrieve values from file errors_list.json,
|
15 | 15 | * errorsList factory returns a promise. */
|
16 | 16 | return $http.get('errors_list.json');
|
17 | 17 | })
|
18 |
| - |
19 |
| - .directive('awesomeError', function (errorsList) { |
| 18 | + .directive('awesomeError', function (list) { |
20 | 19 | return {
|
21 | 20 | restrict: 'E',
|
22 | 21 | replace: true,
|
23 |
| - scope : { |
24 |
| - msg : '=msg' |
| 22 | + scope: { |
| 23 | + target: '=' |
25 | 24 | },
|
26 | 25 | template: '<div> \
|
27 | 26 | <div ng-show="showerr"> \
|
|
36 | 35 | </div> \
|
37 | 36 | </div>',
|
38 | 37 | link: function(scope) {
|
39 |
| - scope.errorMessage = {}; |
40 | 38 |
|
41 |
| - /* fetching data present in 'errors-list.json' file */ |
42 |
| - errorsList.success(function(data) { |
43 |
| - scope.errorMessage = data; |
44 |
| - }); |
| 39 | + scope.messages = {}; |
45 | 40 |
|
46 |
| - scope.$on('show error', function (args) { |
47 |
| - scope.showerr = true; |
48 |
| - }); |
| 41 | + var _showMsg = function(val, args, target, str) { |
| 42 | + if (scope.target === target) { |
| 43 | + scope.showMessage = scope.messages[str]; |
| 44 | + if (scope.showMessage === "") { |
| 45 | + scope.showMessage = scope.messages["Undefined"]; |
| 46 | + } |
| 47 | + if (args.name.indexOf('error') !== -1) { |
| 48 | + scope.showerr = val; |
| 49 | + } else { |
| 50 | + scope.showsucc = val; |
| 51 | + } |
| 52 | + } |
| 53 | + }; |
49 | 54 |
|
50 |
| - scope.$on('hide error', function (args) { |
51 |
| - scope.showerr = false; |
| 55 | + /* fetching data present in 'errors-list.json' file */ |
| 56 | + list.success(function(data) { |
| 57 | + scope.messages = data; |
52 | 58 | });
|
53 | 59 |
|
54 |
| - scope.hideError = function () { |
55 |
| - scope.showerr = false; |
56 |
| - }; |
| 60 | + scope.$on('show error', function (args, target, err) { |
| 61 | + _showMsg(true, args, target, err); |
| 62 | + }); |
57 | 63 |
|
58 |
| - scope.$on('show success', function (args) { |
59 |
| - scope.showsucc = true; |
| 64 | + scope.$on('hide error', function (args, target) { |
| 65 | + _showMsg(false, args, target); |
60 | 66 | });
|
61 | 67 |
|
62 |
| - scope.$on('hide success', function (args) { |
63 |
| - scope.showsucc = false; |
| 68 | + scope.$on('show success', function (args, target, succ) { |
| 69 | + _showMsg(true, args, target, succ); |
64 | 70 | });
|
65 | 71 |
|
66 |
| - scope.$watch(function() {return scope.msg}, function(value) { |
67 |
| - scope.showMessage = scope.errorMessage[scope.msg]; |
68 |
| - if (scope.showMessage === "") { |
69 |
| - scope.$apply(function() { |
70 |
| - scope.showMessage = scope.errorMessage["Undefined"]; |
71 |
| - }); |
72 |
| - } |
| 72 | + scope.$on('hide success', function (args, target) { |
| 73 | + _showMsg(false, args, target); |
73 | 74 | });
|
74 | 75 | }
|
75 | 76 | };
|
76 | 77 | });
|
77 |
| - |
78 | 78 | })(window, window.angular);
|
0 commit comments