AngularJS directives to handle notification and form validation.

1) Checking enough amount at real-time when typing value.
2) Common notifications disappear after 5 seconds.
This commit is contained in:
Gustavo Cortez 2014-04-23 13:16:20 -03:00
commit 014b7310f4
3 changed files with 44 additions and 9 deletions

View file

@ -19,5 +19,44 @@ angular.module('copay')
ctrl.$formatters.unshift(validator);
}
};
}]);
}])
.directive('notification', ['$rootScope', function($rootScope){
return {
restrict: 'A',
link: function(scope, element, attrs, ctrl) {
setTimeout(function(){
scope.$apply(function() {
$rootScope.flashMessage = {};
});
}, 5000);
}
};
}])
.directive('enoughAmount', ['$rootScope', function($rootScope){
return {
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var val = function(value) {
var vStr = new String(value);
var vNum = Number(vStr);
if (typeof vNum == "number" && vNum > 0) {
if ($rootScope.availableBalance <= vNum) {
ctrl.$setValidity('enoughAmount', false);
$rootScope.notEnoughAmount = 'Insufficient funds!';
}
else {
ctrl.$setValidity('enoughAmount', true);
$rootScope.notEnoughAmount = null;
}
} else {
$rootScope.notEnoughAmount = null;
}
return value;
}
ctrl.$parsers.unshift(val);
ctrl.$formatters.unshift(val);
}
};
}])
;