Skip to content

Commit 2129c8f

Browse files
committed
awesome error updated
1 parent 1a7838f commit 2129c8f

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ Usage
99
* **Requires Twitter Bootstrap**
1010

1111
* Include the directive in your template as,
12-
* <awesome-error msg="errorMessage"></awesome-error> where, errorMessage is in the scope
12+
* <awesome-error target="'settingsPage'"></awesome-error> where, target defines the error directive you are targetting.
1313
* Then, broadcast or emit the message from controllers or directives. e.g.
14-
* $scope.$emit('show error'); // to show error msg
15-
* $scope.$emit('hide error'); // to hide error msg
16-
* $scope.$emit('show success'); // to show success msg
17-
* $scope.$emit('hide success'); // to hide success msg
14+
* $scope.$broadcast(errorState, target, error); // template of broadcast
15+
* $scope.$broadcast('show error', 'settingsPage', 'INVALID_DATE'); // to show error msg
16+
* $scope.$emit('hide error', 'settingsPage'); // to hide error msg
17+
* $scope.$emit('show success', 'settingsPage', 'TWEET_POSTED'); // to show success msg
18+
* $scope.$emit('hide success', 'settingsPage'); // to hide success msg
1819

1920
You need to keep all your error messages in a JSON file 'errors_list.json'. e.g.
2021
```
2122
{
2223
"INVALID_DATE" : "Please enter a valid date",
23-
"username_not_found" : "Sorry, This username is already taken"
24+
"username_not_found" : "Sorry, This username is already taken",
25+
"TWEET_POSTED": "Tweet was posted successfully"
2426
}
2527
```
2628

angular-awesome-error.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@
99
'use strict';
1010

1111
angular.module('ngAngularError', ['ng'])
12-
.factory('errorsList', function($http) {
12+
.factory('list', function($http) {
1313
/* errors_list.json should contain a map of errors and user-feedback.
1414
* we use, $http to retrieve values from file errors_list.json,
1515
* errorsList factory returns a promise. */
1616
return $http.get('errors_list.json');
1717
})
18-
19-
.directive('awesomeError', function (errorsList) {
18+
.directive('awesomeError', function (list) {
2019
return {
2120
restrict: 'E',
2221
replace: true,
23-
scope : {
24-
msg : '=msg'
22+
scope: {
23+
target: '='
2524
},
2625
template: '<div> \
2726
<div ng-show="showerr"> \
@@ -36,43 +35,44 @@
3635
</div> \
3736
</div>',
3837
link: function(scope) {
39-
scope.errorMessage = {};
4038

41-
/* fetching data present in 'errors-list.json' file */
42-
errorsList.success(function(data) {
43-
scope.errorMessage = data;
44-
});
39+
scope.messages = {};
4540

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+
};
4954

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;
5258
});
5359

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+
});
5763

58-
scope.$on('show success', function (args) {
59-
scope.showsucc = true;
64+
scope.$on('hide error', function (args, target) {
65+
_showMsg(false, args, target);
6066
});
6167

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);
6470
});
6571

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);
7374
});
7475
}
7576
};
7677
});
77-
7878
})(window, window.angular);

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-errors-directive",
3-
"version": "0.0.10",
3+
"version": "0.2.0",
44
"main": "angular-awesome-error.js",
55
"ignore": [
66
"**/.*",

0 commit comments

Comments
 (0)