Advertisement
KzDrew

sandbox_ajax_forms.js

Aug 15th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // -- mobileinit event --
  2.     // -- this code is executed once, when jquery mobile is loaded (and must be placed before jqm is loaded)
  3.     // -- this is where you can change default options or behavior
  4.     $(document).bind('mobileinit', function() {
  5.         console.log('mobileinit event');
  6.         //$.mobile.page.prototype.options.domCache = true;
  7.     });
  8.  
  9.  
  10.     $(function() {
  11.  
  12.  
  13.             console.log('jquery document ready function');
  14.  
  15.  
  16.             $(document).on('pageinit', '#formPage', function(){
  17.  
  18.                 $(document).on('click', '#submit', function() { // catch the form's submit event
  19.  
  20.                     console.log('#submit click handler');
  21.  
  22.                     if($('#name').val().length > 0 && $('#email').val().length > 0 && $('#memory').val().length > 0){
  23.  
  24.                         console.log('passed basic validation');
  25.  
  26.                         //var that = $(this),
  27.                         contents = $('#form').serialize();
  28.  
  29.                         console.log('contents');
  30.                         console.log(contents);
  31.  
  32.  
  33.                         // Send data to server through ajax call
  34.                         // action is functionality we want to call and outputJSON is our data
  35.  
  36.  
  37.                         $.ajax({
  38.                             url: 'https://someserver.com/av/mobile/sandbox_ajax_form.php',
  39.                             dataType: 'json',
  40.                             type: 'post',
  41.                             data: contents,
  42.                             async: true,
  43.                             beforeSend: function() {
  44.                                 // This callback function will trigger before data is sent
  45.                                 $.mobile.showPageLoadingMsg(true); // This will show ajax spinner
  46.                             },
  47.                             complete: function() {
  48.                                 // This callback function will trigger on data sent/received complete
  49.                                 $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner
  50.                             },
  51.  
  52.                             success: function(data) {
  53.                                 console.log(data);
  54.                                 if(data.success){
  55.                                     alert('Hello '+data.result);
  56.                         }else{
  57.                             alert('Did not get a good result');
  58.                         }
  59.  
  60.                             },
  61.                             error: function (request,error) {
  62.                                 // This callback function will trigger on unsuccessful action
  63.                                 alert('Network error has occurred please try again!');
  64.                             }
  65.                         });
  66.  
  67.  
  68.                     } else {
  69.                         console.log('failed basic validation');
  70.                         alert('Please fill all nececery fields');
  71.                     }
  72.                     return false; // cancel original event to prevent form submitting
  73.                 });
  74.             });
  75.  
  76.  
  77.  
  78.             // -- pagebeforechange event --
  79.             // -- this code is executed every time ANY page (cached or new) is about to be loaded, BEFORE the page is displayed/loaded
  80.             // -- this is where you can see what page was requested,  stop the request, or alter the request
  81.             $(document).on( "pagebeforechange", function( event, data ){
  82.                 if ( typeof data.toPage === "string" ) {
  83.                     console.log('pagebeforechange event - ALL pages');
  84.  
  85.                     console.log(data);
  86.  
  87.                     // .. do stuff here
  88.                 }
  89.             });
  90.  
  91.             // -- pageinit event that fires for SPECIFIC PAGE ONLY --
  92.             // -- this code is executed only once upon initital page load (or if it is no longer in the cache)
  93.             $(document).on("pageinit", "#formPage", function() {
  94.                 console.log('pageinit event - #formPage only');
  95.                 // .. do stuff for example page one here
  96.  
  97.  
  98.             });
  99.  
  100.  
  101.             // -- pageshow event --
  102.             // -- this code is executed every time ANY page is shown (cached or new)
  103.             // -- this is where you can manipulate the contents of the page being shown - think of it as JQM's version of jquery's $(function(){ .. }
  104.             $(document).on( "pageshow", function(){
  105.                 console.log('pageshow event - ALL pages');
  106.                 // .. do stuff here
  107.             });
  108.  
  109.             // -- pageshow event that fires for SPECIFIC PAGE ONLY --
  110.             // -- the second parameter targets the id value of the page div used in the body section, aka: <div data-role="page" id="example_page_one" ..> .. </div>
  111.             // -- this code is executed every time the 'example_page_one' page is loaded (cached or new)
  112.             // -- we include code for all the different pages at once in the head because jqm will only every process the head .. /head content ONCE and never again
  113.             $(document).on("pageshow", "#formPage", function() {
  114.                 console.log('pageshow event - #example_page_one only');
  115.                 // .. do stuff for example page one here
  116.             });
  117.  
  118.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement