From 5ba9b6a454a3700410979439a1267138ddd16737 Mon Sep 17 00:00:00 2001 From: Anton Pisarenko Date: Sat, 7 Feb 2015 11:53:45 +0300 Subject: [PATCH 1/2] add configuration for errorCode --- .gitignore | 3 +++ src/angular-server-form.js | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 74831c9..7a9fce2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ bower_components # OS X .DS_Store + +# IDEs +.idea diff --git a/src/angular-server-form.js b/src/angular-server-form.js index d562df7..8ce6240 100644 --- a/src/angular-server-form.js +++ b/src/angular-server-form.js @@ -7,7 +7,9 @@ var provider = this; provider.errorsKey = 'errors'; + provider.errorCode = 422; provider.logging = true; + provider.$get = ['$http', '$q', '$log', function ($http, $q, $log) { // Private @@ -112,7 +114,7 @@ }) .error(function(res, status) { form.$saved = false; - if (status === 422) { + if (status === provider.errorCode) { if (provider.errorsKey) { self.applyErrors(form, res[provider.errorsKey]); } else { From 0293df691610533e6248eb3a15ab18dbf54ea5a7 Mon Sep 17 00:00:00 2001 From: Anton Pisarenko Date: Sat, 7 Feb 2015 12:07:19 +0300 Subject: [PATCH 2/2] build dist --- dist/angular-server-form.js | 4 +++- dist/angular-server-form.min.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dist/angular-server-form.js b/dist/angular-server-form.js index d562df7..8ce6240 100644 --- a/dist/angular-server-form.js +++ b/dist/angular-server-form.js @@ -7,7 +7,9 @@ var provider = this; provider.errorsKey = 'errors'; + provider.errorCode = 422; provider.logging = true; + provider.$get = ['$http', '$q', '$log', function ($http, $q, $log) { // Private @@ -112,7 +114,7 @@ }) .error(function(res, status) { form.$saved = false; - if (status === 422) { + if (status === provider.errorCode) { if (provider.errorsKey) { self.applyErrors(form, res[provider.errorsKey]); } else { diff --git a/dist/angular-server-form.min.js b/dist/angular-server-form.min.js index 4f7cd43..1d3a331 100644 --- a/dist/angular-server-form.min.js +++ b/dist/angular-server-form.min.js @@ -1 +1 @@ -!function(r,e){"use strict";e.module("angular-server-form",[]).provider("serverForm",[function(){var r=this;r.errorsKey="errors",r.logging=!0,r.$get=["$http","$q","$log",function(n,o,t){function i(r,e){(r.$render||r.$setViewValue)&&r.$setValidity("server",!1),r.$setPristine(),r.$server=e}function s(r){var e={};for(var n in r)r.hasOwnProperty(n)&&"$"!==n[0]&&(e[n]=r[n].hasOwnProperty("$modelValue")?r[n].$modelValue:s(r[n]));return e}var a=this;return a.serialize=function(r){var e={};return r.$name?e[r.$name]=s(r):e=s(r),e},a.applyErrors=function(n,o){if(e.isString(o))i(n,o);else if(e.isArray(o))i(n,o.join(", "));else for(var s in o)o.hasOwnProperty(s)&&(n.hasOwnProperty(s)?a.applyErrors(n[s],o[s]):n.$name===s?a.applyErrors(n,o[s]):r.logging&&t.warn("No place to render property",s,"in form",n))},a.clearErrors=function u(r){(r.$render||r.$setViewValue)&&r.$setValidity("server",!0),r.$setPristine(),r.$server="";for(var e in r)r.hasOwnProperty(e)&&"$"!==e[0]&&u(r[e])},a.submit=function(e,o){return a.clearErrors(e),e.$submitting=!0,e.$saved=!1,n(o).success(function(){e.$saved=!0}).error(function(n,o){e.$saved=!1,422===o&&(r.errorsKey?a.applyErrors(e,n[r.errorsKey]):a.applyErrors(e,n))}).finally(function(){e.$submitting=!1})},a}]}]).directive("serverForm",["serverForm",function(r){return{restrict:"A",scope:{url:"@",method:"@",onSuccess:"=",onError:"="},require:"form",link:function(e,n,o,t){function i(n){e.$apply(function(){if(!t.$submitting){var o={url:e.url,method:e.method?e.method:"POST",data:r.serialize(t)};r.submit(t,o).then(e.onSuccess,e.onError)}n.preventDefault()})}n.on("submit",i),e.$on("destroy",function(){n.off("submit",i)})}}}])}(window,window.angular); \ No newline at end of file +!function(r,e){"use strict";e.module("angular-server-form",[]).provider("serverForm",[function(){var r=this;r.errorsKey="errors",r.errorCode=422,r.logging=!0,r.$get=["$http","$q","$log",function(n,o,t){function i(r,e){(r.$render||r.$setViewValue)&&r.$setValidity("server",!1),r.$setPristine(),r.$server=e}function s(r){var e={};for(var n in r)r.hasOwnProperty(n)&&"$"!==n[0]&&(e[n]=r[n].hasOwnProperty("$modelValue")?r[n].$modelValue:s(r[n]));return e}var a=this;return a.serialize=function(r){var e={};return r.$name?e[r.$name]=s(r):e=s(r),e},a.applyErrors=function(n,o){if(e.isString(o))i(n,o);else if(e.isArray(o))i(n,o.join(", "));else for(var s in o)o.hasOwnProperty(s)&&(n.hasOwnProperty(s)?a.applyErrors(n[s],o[s]):n.$name===s?a.applyErrors(n,o[s]):r.logging&&t.warn("No place to render property",s,"in form",n))},a.clearErrors=function u(r){(r.$render||r.$setViewValue)&&r.$setValidity("server",!0),r.$setPristine(),r.$server="";for(var e in r)r.hasOwnProperty(e)&&"$"!==e[0]&&u(r[e])},a.submit=function(e,o){return a.clearErrors(e),e.$submitting=!0,e.$saved=!1,n(o).success(function(){e.$saved=!0}).error(function(n,o){e.$saved=!1,o===r.errorCode&&(r.errorsKey?a.applyErrors(e,n[r.errorsKey]):a.applyErrors(e,n))}).finally(function(){e.$submitting=!1})},a}]}]).directive("serverForm",["serverForm",function(r){return{restrict:"A",scope:{url:"@",method:"@",onSuccess:"=",onError:"="},require:"form",link:function(e,n,o,t){function i(n){e.$apply(function(){if(!t.$submitting){var o={url:e.url,method:e.method?e.method:"POST",data:r.serialize(t)};r.submit(t,o).then(e.onSuccess,e.onError)}n.preventDefault()})}n.on("submit",i),e.$on("destroy",function(){n.off("submit",i)})}}}])}(window,window.angular); \ No newline at end of file