pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

commit fad9bad987996af45c0bc1ba1ecdd0f877e80888
parent ff22607223c491ec7791113b6d93a020dbffbc88
Author: Jul <jul@9o.is>
Date:   Wed, 28 May 2014 19:07:26 -0400

modularized FormCtrl's save (now submit) function

Diffstat:
Msrc/main/webapp/app/App.js | 221++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 111 insertions(+), 110 deletions(-)

diff --git a/src/main/webapp/app/App.js b/src/main/webapp/app/App.js @@ -96,6 +96,26 @@ app.controller('FormCtrl', ['$scope', function($scope) { $scope.stateSuccessError = function(el) { return "{'state-error':form."+el+".$invalid && !form."+el+".$pristine,'state-success':form."+el+".$valid && !form."+el+".$pristine}"; }; + + /* Sends data to server. */ + $scope.submit = function(className, funcName, successFunc, failureFunc) { + $scope.loading = true; + window[className][funcName]($scope.model).then(function(alert) { + $scope.$apply(function() { + window.console.log("in the zone: "+alert.msg); + $scope.loading = false; + if(alert.msg_type === "success") { + if(typeof(successFunc) === "function") { + successFunc(alert); + } + } else { + if(typeof(failureFunc) === "function") { + failureFunc(alert); + } + } + }); + }); + }; /* Resets client-side data. */ $scope.reset = function() { @@ -170,41 +190,39 @@ app.controller('AutoUpdateFormCtrl', ['$scope', '$controller', '$rootScope', fun app.controller('NearAtmNotifyCtrl', ['$scope', '$controller', '$rootScope', function($scope, $controller, $rootScope) { $controller('FormCtrl', {$scope: $scope}); + $scope.zip_code_regex = ZIP_CODE_REGEXP; - $scope.save = function() { - $scope.loading = true; - window.NearAtmNotify.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - $rootScope.$broadcast('alertDialog', alert); - $scope.reset(); - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); + $scope.save = function() { + var success = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + $scope.reset(); + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('NearAtmNotify', 'submit', success, failure); }; }]); app.controller('AtmApplicationCtrl', ['$scope', '$controller', '$rootScope', function($scope, $controller, $rootScope) { $controller('FormCtrl', {$scope: $scope}); - $scope.url_regex = UNSAFE_URL_REGEXP; - $scope.save = function() { - $scope.loading = true; - window.AtmApplication.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - $rootScope.$broadcast('alertDialog', alert); - $scope.reset(); - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); + $scope.url_regex = UNSAFE_URL_REGEXP; + + $scope.save = function() { + var success = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + $scope.reset(); + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('AtmApplication', 'submit', success, failure); }; }]); @@ -212,37 +230,33 @@ app.controller('UserRegistrationCtrl', ['$scope', '$controller', '$rootScope', f $controller('FormCtrl', {$scope: $scope}); $scope.save = function() { - $scope.loading = true; - window.UserRegistration.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - $rootScope.$broadcast('alertDialog', alert); - $scope.reset(); - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); + var success = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + $scope.reset(); + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('UserRegistration', 'submit', success, failure); }; }]); app.controller('PasswordRecoveryCtrl', ['$scope', '$controller', '$rootScope', function($scope, $controller, $rootScope) { $controller('FormCtrl', {$scope: $scope}); - $scope.save = function() { - $scope.loading = true; - window.PasswordRecovery.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - $rootScope.$broadcast('alertDialog', alert); - $scope.reset(); - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); + $scope.save = function() { + var success = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + $scope.reset(); + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('PasswordRecovery', 'submit', success, failure); }; }]); @@ -251,43 +265,35 @@ app.controller('UserLoginCtrl', ['$scope', '$controller', '$rootScope', function $scope.model = {remember: false}; - $scope.save = function() { - $scope.loading = true; - window.UserLogin.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - window.location.href = "/settings"; - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); + $scope.save = function() { + var success = function() { + window.location.href = "/settings"; + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('UserLogin', 'submit', success, failure); }; }]); -app.controller('FindAtmCtrl', ['$scope', '$state', '$rootScope', function($scope, $state, $rootScope) { +app.controller('FindAtmCtrl', ['$scope', '$state', '$rootScope', '$controller', function($scope, $state, $rootScope, $controller) { + $controller('FormCtrl', {$scope: $scope}); + $scope.zip_code_regex = ZIP_CODE_REGEXP; - $scope.model = {}; $scope.search = function() { - $scope.loading = true; - window.FindAtm.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - $state.go('atmlocations'); - $scope.reset(); - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); - }; - - $scope.reset = function() { - $scope.zip_code = {}; - $scope.form.$setPristine(); + var success = function() { + $state.go('atmlocations'); + $scope.reset(); + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('FindAtm', 'submit', success, failure); }; }]); @@ -297,19 +303,17 @@ app.controller('PasswordChangeCtrl', ['$scope', '$controller', '$rootScope', fun $scope.password_regex = PASSWORD_REGEXP; $scope.save = function() { - $scope.loading = true; - window.PasswordChange.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - $rootScope.$broadcast('alertDialog', alert); - $scope.reset(); - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); - }; + var success = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + $scope.reset(); + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('PasswordChange', 'submit', success, failure); + }; }]); app.controller('UserSettingsCtrl', ['$scope', '$controller', '$rootScope', function($scope, $controller, $rootScope) { @@ -325,20 +329,17 @@ app.controller('UserSettingsEmailCtrl', ['$scope', '$controller', '$rootScope', $scope.init('UserSettingsEmail', 'init'); $scope.save = function() { - $scope.loading = true; - window.UserSettingsEmail.submit($scope.model).then(function(alert) { - $scope.$apply(function() { - $scope.loading = false; - if(alert.msg_type === "success") { - $rootScope.$broadcast('alertDialog', alert); - $scope.reset(); - - } else { - $rootScope.$broadcast('alertDialog', alert); - } - }); - }); - }; + var success = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + $scope.reset(); + }; + + var failure = function(alert) { + $rootScope.$broadcast('alertDialog', alert); + }; + + $scope.submit('UserSettingsEmail', 'submit', success, failure); + }; }]); app.controller('GMapCtrl', ['$scope', function($scope) {