Monday, January 23, 2012

break up a big GET AJAX call into chunks

Here is another solution for the problem described here. In this approach I am breaking a GET AJAX call up into chunks. Hmmm. I'm realizing that I went about my POST AJAX call the wrong way as I was still crafting a URL line. Ah, but I digress. Does it seem silly to even use an AJAX call to an MVC action? For this code, it sure does. I'm assuming that the code that my friend is working does more sanity checking than I am doing and that the action call by way of AJAX is just needed, but then there is an old joke about the word assume.

<form id="SubmissionFacilitator" method="POST"

      action="/home/successfulsubmission/">

   <textarea name="BlobOfCopy"></textarea>

   <div id="ErrorHint" style="color: #CC0000;"></div>

   <input type="submit" value="submit"/>

</form>

<script type="text/javascript">

   $(function () {

      var wrapper = $('#SubmissionFacilitator');

      var textarea = wrapper.find('textarea[name=BlobOfCopy]');

      $(textarea).keyup(function () {

         var junkbool = assesssanityconcerns($.trim(textarea.val()));

      });

      $('#SubmissionFacilitator').submit(function () {

         return assesssanityconcerns($.trim(textarea.val()));

      });

   });

   function assesssanityconcerns(content) {

      var maxsize = 5000;

      if (content.length > maxsize) {

         var gap = content.length - maxsize;

         var error = "There are " + gap + " too many characters.";

         $('#ErrorHint').html(error);

         return false;

      } else {

         if (content.length > 1000) {

            var one = "";

            var two = "";

            var three = "";

            var four = "";

            var five = "";

            for (i = 0; i < 1000; i++) {

               one = one + content.charAt(i);

            }

            for (i = 1000; i < 2000; i++) {

               if (i < content.length) {

                  two = two + content.charAt(i);

               }

            }

            if (content.length > 2000) {

               for (i = 2000; i < 3000; i++) {

                  if (i < content.length) {

                     three = three + content.charAt(i);

                  }

               }

            }

            if (content.length > 3000) {

               for (i = 3000; i < 4000; i++) {

                  if (i < content.length) {

                     four = four + content.charAt(i);

                  }

               }

            }

            if (content.length > 4000) {

               for (i = 4000; i < 5000; i++) {

                  if (i < content.length) {

                     five = five + content.charAt(i);

                  }

               }

            }

            var url = "@ViewBag.WhereAmI";

            url = url + "/home/validate?value=" + one;

            url = url + "&isRequired=true";

            url = url + "&maxSize=" + maxsize;

            var message = $.ajax({

               url: url,

               async: false

            }).responseText;

            url = "@ViewBag.WhereAmI";

            url = url + "/home/validate?value=" + two;

            url = url + "&isRequired=true";

            url = url + "&maxSize=" + maxsize;

            var messagealt = $.ajax({

               url: url,

               async: false

            }).responseText;

            if (message != messagealt) {

               if (message == "good") {

                  message = messagealt;

               }

            }

            if (three != "") {

               url = "@ViewBag.WhereAmI";

               url = url + "/home/validate?value=" + three;

               url = url + "&isRequired=true";

               url = url + "&maxSize=" + maxsize;

               messagealt = $.ajax({

                  url: url,

                  async: false

               }).responseText;

               if (message != messagealt) {

                  if (message == "good") {

                     message = messagealt;

                  }

               }

            }

            if (four != "") {

               url = "@ViewBag.WhereAmI";

               url = url + "/home/validate?value=" + four;

               url = url + "&isRequired=true";

               url = url + "&maxSize=" + maxsize;

               messagealt = $.ajax({

                  url: url,

                  async: false

               }).responseText;

               if (message != messagealt) {

                  if (message == "good") {

                     message = messagealt;

                  }

               }

            }

            if (five != "") {

               url = "@ViewBag.WhereAmI";

               url = url + "/home/validate?value=" + five;

               url = url + "&isRequired=true";

               url = url + "&maxSize=" + maxsize;

               messagealt = $.ajax({

                  url: url,

                  async: false

               }).responseText;

               if (message != messagealt) {

                  if (message == "good") {

                     message = messagealt;

                  }

               }

            }

            if (message == "good") {

               $('#ErrorHint').html("");

               return true;

            } else {

               $('#ErrorHint').html(message);

               return false;

            }

         } else {

            var url = "@ViewBag.WhereAmI";

            url = url + "/home/validate?value=" + content;

            url = url + "&isRequired=true";

            url = url + "&maxSize=" + maxsize;

            var message = $.ajax({

               url: url,

               async: false

            }).responseText;

            if (message == "good") {

               $('#ErrorHint').html("");

               return true;

            } else {

               $('#ErrorHint').html(message);

               return false;

            }

         }

      }

   }

</script>

No comments:

Post a Comment