Skip to main content

Jquery 2 - part 2































$(document).ready(function() {
  $('#tour').on('click', 'button', function() {
    $.ajax('/photos.html', {
      success: function(response) {
        $('.photos').html(response).fadeIn();
      }
    });
  });
});

$(document).ready(function() {
  $('#tour').on('click', 'button', function() {
    $.get('/photos.html', function(response) {
        $('.photos').html(response).fadeIn();
    });
  });

});

-----------------------------------------------------------------------------------------------------------


data-location="london">



$(document).ready(function() {
  var el = $("#tour")
  el.on("click", "button", function() {
    $.ajax('/photos.html', {
      data: {location: el.data('location')},
      success: function(response) {
        $('.photos').html(response).fadeIn();
      }


$(document).ready(function() {
  $("#tour").on("click", "button", function() {
    $.ajax('/photos.html', {
      data:{location: $("#tour").data('location')},
      success: function(response) {
        $('.photos').html(response).fadeIn();
       
      }
    });
  });
});






























$(document).ready(function() {
  var el = $("#tour");
  el.on("click", "button", function() {
    $.ajax('/photos.html', {
      data: {location: el.data('location')},
      success: function(response) {
        $('.photos').html(response).fadeIn();
      },
      error:function(request,errorType,errorMessage){
      $('.photos').html('
  • You lost connection
  • '); } }); }); });
    
    
    
    
    
    
    $(document).ready(function() {
      $("#tour").on("click", "button", function() {
        $.ajax('/photos.html', {
          success: function(response) {
            $('.photos').html(response).fadeIn();
          },
          error: function() {
            $('.photos').html('
    
  • There was a problem fetching the latest photos. Please try again.
  • '); }, timeout: 3000, beforeSend: function(){ $('#tour').addClass('is-fetching'); }, complete:function(){ $('#tour').removeClass('is-fetching'); } }); }); });
    
    
    
    
    
    
    $(document).ready(function() {
      function showPhotos() {
        $(this).find('span').slideToggle();
      }
      $('.photos li').on('mouseenter', showPhotos)
                     .on('mouseleave', showPhotos);
    
    
      var el = $("#tour");
      el.on("click", "button", function() {
        $.ajax('/photos.html', {
          data: {location: el.data('location')},
          success: function(response) {
            $('.photos').html(response).fadeIn();
          },
          error: function() {
            $('.photos').html('
    
  • There was a problem fetching the latest photos. Please try again.
  • '); },
    timeout: 3000, beforeSend: function() { $('#tour').addClass('is-fetching'); }, complete: function() { $('#tour').removeClass('is-fetching'); } }); }); });


    function Tour(el) {
      var tour = this;
      this.el = el;
      this.fetchPhotos = function() { 
        $.ajax('/photos.html', {
          context: tour,
          data: {location: el.data('location')},
          success: function(response) {
            this.el.find('.photos').html(response).fadeIn();
          },
          error: function() {
            this.el.find('.photos').html('
    
  • problem fetching Please.
  • '
    ); }, timeout: 3000, beforeSend: function() { this.el.addClass('is-fetching'); }, complete: function() { this.el.removeClass('is-fetching'); } }); } this.el.on('click', 'button', this.fetchPhotos); }$(document).ready(function() { var paris = new Tour($('#paris')); });
    
    




    CHAPTER 2

    JavaScript and jQuery



    JavaScript Objects

    1.  2.2 JavaScript Objects
    2.  2.3 Refactor To Objects
    3.  2.4 Event Handler Refactor



















    var tour = {
      init: function() {
        $("#tour").on("click", "button", function() { 
          $.ajax('/photos.html', {
            data: {location: $("#tour").data('location')},
            success: function(response) {
              $('.photos').html(response).fadeIn();
            },
            error: function() {
              $('.photos').html('









  • There was a problem fetching the latest photos. Please try again.
  • ');
            },
            timeout: 3000,
            beforeSend: function() {
              $('#tour').addClass('is-fetching');
            },
            complete: function() {
              $('#tour').removeClass('is-fetching');
            }
          });
        });
      }
    }
    $(document).ready(function() { 
      tour.init();
    });




























    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     WITH FORMS













    function Tour(el) {
      var tour = this;
      this.el = el;
      this.fetchPhotos = function() { 
        $.ajax('/photos.html', {
          data: {location: tour.el.data('location')},
          context: tour,
          success: function(response) {
            this.el.find('.photos').html(response).fadeIn();
          },
          error: function() {
            this.el.find('.photos').html('







  • There was a problem fetching the latest photos. Please try again.
  • ');
          },
          timeout: 3000,
          beforeSend: function() {
            this.el.addClass('is-fetching');
          },
          complete: function() {
            this.el.removeClass('is-fetching');
          }
        });
      }
      this.el.on('click', 'button', this.fetchPhotos);
    }
    $(document).ready(function() { 
      var paris = new Tour($('#paris'));
      var london = new Tour($('#london'));
    });

    ----------------------------------------------------------------------------------------------------------------------









    CHAPTER 3

    Ajax With Forms


    Ajax With Forms

    1.  3.2 Ajax With Forms
    2.  3.3 Form Submit Event
    3.  3.4 $.ajax with POST
    4.  3.5 Success Callback



























    $(document).ready(function() {
      $('form').on('submit', function(event) {
        event.preventDefault();
        $.ajax('/book', {
          type: 'POST',
          data: $('form').serialize(),
          success:function(response){
            $('.tour').html(response)
          }
        });
      });
    });


    Ajax With JSON

    1.  3.6 Ajax With JSON
    2.  3.7 The JSON Switch
    3.  3.8 Don't Repeat Yourself



    $(document).ready(function() {
      $('form').on('submit', function(event) {
        event.preventDefault();
        $.ajax($('form').attr('action'), {
          type: $('form').attr('method'),
          data: $('form').serialize(),
          dataType: 'json',
          success: function(response) {
            $('.tour').html('
    ')

                      .find('p')
                      .append('Trip to ' + response.description)
                      .append(' at $' + response.price)
                      .append(' for ' + response.nights + ' nights')
                      .append('. Confirmation: ' +  response.confirmation);
          }
        });
      });
    });




    $('.update-available-flights').on('click', function() {
      $.getJSON('/flights/late', function(result) {
        var flightElements = $.map(result, function(flightItem, index){
          var flightEl = $('
    
  • '
  • +flightItem.flightNumber+'-'+flightItem.time+'
    ');
    return flightEl;
    });
    $('.flight-times').detach()
    .html(flightElements)
    .appendTo($('.flights'))
    });
    });































    $('button').on('click', function() {
      $.ajax('/cities/deals', {
        type: 'get',
        success: function(result) {
          $.each(result, function(index, dealItem) {
            var dealElement = $('.deal-' + index);
            dealElement.find('.name').html(dealItem.name);
            dealElement.find('.price').html(dealItem.price);
          });
        }
      });
    });
    
    
    CODE REFACTOR

    $('button').on('click', function() {
      $.getJSON('/cities/deals',  function(result) {
          $.each(result, function(index, dealItem) {
            var dealElement = $('.deal-' + index);
            dealElement.find('.name').html(dealItem.name);
            dealElement.find('.price').html(dealItem.price);
          });
        }
      );

    });


    $('.update-available-flights').on('click', function() {
      $.getJSON('/flights/late', function(result) {
        $.map(result,function(dealItem,index){
                console.log(dealItem);
        });

      });
    });



     
       

      $('.update-available-flights').on('click', function() {
        $.getJSON('/flights/late', function(result) {
          var flightElements = $.map(result, function(flightItem, index){
            return $('
      
    • '
    • +flightItem.flightNumber+'-'+flightItem.time+'
      ');
      });
      $('.flight-times').html(flightElements);
      });
      });

      CHAPTER 5






      jQuery Plugins


      Level Points






























































      $(document).ready(function(){
        // Get Weather
        $('button').on('click.weather', function() {
          var results = $(this).closest('li').find('.results');
          results.append('Weather: 74°
      ');

          $(this).off('click.weather');
        });
        
         // Show Photos
        $('button').on('click.photos', function() {
          var tour = $(this).closest('li');
          var results = tour.find('.results');
          results.append('
      ');

          $(this).off('click.photos');
        });
      });

      custom events 

      $(document).ready(function(){
        // Get Weather
        $('button').on('show.weather', function() {
          var results = $(this).closest('li').find('.results');
          results.append('Weather: 74°
      ');

          $(this).off('show.weather');
        });

        // Show Photos
        $('button').on('click.photos', function() {
          var tour = $(this).closest('li');
          var results = tour.find('.results');
          results.append('
      ');

          $(this).off('click.photos');
          $(this).trigger('show.weather');
        });

      });
      -------------------------------------------------------------------------------------------------------------------------








































      -------------------------------------------------------------------------------------------------------------------------



      CHAPTER 6

      Promises

      Badge-06



      For using ajax call on multiple pages 


































      var Vacation = {
        
       getPrice :function(location){
         
          var promise =  $.ajax('/vacation/prices', {
            data: {q: location},
            success: function(result){
              promise.resolve(result.price);
            }
          
          } );
          return promise;
        }
      };
      ------------------------------------------------------------------

      var Vacation = {
        getPrice: function(location){
          var promise = $.Deferred();
          $.ajax('/vacation/prices', {
            data: {q: location},
            success: function(result){
             promise.resolve(result.price)
            }
          });
          return promise;
        }

      }
      $(document).ready(function() {
        $('button').on('click', function(){
          var location = $('.location').text();
          Vacation.getPrice(location).done(function(result){
            $('.price').text(result);
          }).fail(function(result){
             console.log(result);
          })
        });
      });

      ------------------------------------------------------------------------------------------------------------------------
      ------------------------------------------------------------------------------------------------------------------------

      $(document).ready(function() {
        $('button').on('click', function(){
          var tour = $(this).parent();
          var location = tour.data('location');
          var resultDiv = tour.find('.results').empty();
          Vacation.getPrice(location).done(function(priceResult){
            $('$'+priceResult+'
      ').appendTo(resultDiv);

          });

          Photo.getPhoto(location).done(function(photoResult){
            $('').attr('src', photoResult).appendTo(resultDiv);
          });
        });
      });


      $(document).ready(function() {
        $('button').on('click', function(){
          var location = $(this).parent().data('location');
          var resultDiv = $(this).parent().find('.results').empty();

          $.when(
            Vacation.getPrice(location),
            Photo.getPhoto(location)
          ).then(function(priceResult, photoResult) {
            $('$'+priceResult+'
      ').appendTo(resultDiv);

            $('').attr('src', photoResult).appendTo(resultDiv);
          });
        });
      });

      ------------------------------------------------------------------------------------------------------------------------
      ------------------------------------------------------------------------------------------------------------------------

      $.fn.photofy = function() {
        this.each(function() {
          var tour = $(this);
          var show = function(e) {
             e.preventDefault();
             tour.addClass('is-showing-photofy');
           }
      
           tour.on('click.photofy', '.see-photos', show);
        });
      }
      
      $(document).ready(function() {
        $('.tour').photofy();
      });





      $.fn.photofy = function(options) {
        this.each(function() {
          var show = function(e) {
             e.preventDefault();
             settings.tour
                     .addClass('is-showing-photofy')
                     .find('.photos')
                     .find('li:gt('+(settings.count-1)+')').hide();
           }
           var settings = $.extend({
             count: 3,
             tour: $(this)
           }, options);
           settings.tour.on('click.photofy', '.see-photos', show);
        });
      }
      
      $(document).ready(function() {
        $('.tour').photofy(
      
      
      
      
      
      
      
      
      
      
      $.fn.photofy = function(options) {
        this.each(function() {
          var show = function(e) {
            e.preventDefault();
            settings.tour
                    .addClass('is-showing-photofy')
                    .find('.photos')
                    .find('li:gt('+(settings.photos-1)+')')
                    .hide();
          };
          var settings = $.extend({
            count: 3,
            tour: $(this)
          }, options);
          settings.tour.on('click.photofy', '.see-photos', show);
          settings.tour.on('show.photofy', show);
        });
      }
      
      $(document).ready(function() {
        $('.tour').photofy({ count: 1});
      
        $('.show-photos').on('click', function(e) {
          e.preventDefault();
          $('.tour').trigger('show.photofy');
        });
      });{ count: 1});
      });
      --------------------------------------------------------------------------------------------------------------------------











      Comments

      Popular posts from this blog

      The BattleGround

      Last Sunday while zapping the channels of my idiot box I chanced upon to watch one of the greatest epic of our times –MAHABHARAT. It did’nt take me much time to recall the waves that it created across the nation- when it was telecasted for the first time.I was too young to comprehend it,yet I used to watch it with paramount interest,sitting next to dad & imitating his stimuli to what character speaks Later in my teens ,unlike most of my friends who used to enjoy watching top tens & horror shows , I was inundated to discover that zee was going to re telecast Mahabharat .This time, I was grown up enough to understand it,so I religiously watched all the episodes with same interest. Watching such an epic in formative years has great influence on one’s approach towards life .Today to me ,Mahabharat isn’t just another epic but it is a thinking process, where the mind is the battleground, THE KURUSHETRA ,where all kind of thoughts hover, the state of mind that we...

      WHERE THE MIND IS WITHOUT FEAR !

      (Painting By Takshila Vidushi) "Where the mind is without fear & the head is held high Where knowledge is free Where the world hasn't been broken up into fragments By narrow domestic walls Where words come out from the depth of truth Where tireless striving stretches its arms towards perfection ! Where the clear stream of reason has not lost its way Into the dreary desert sand of dead habit Where the mind is led forward by thee Into ever-widening thought and action Into that heaven of freedom, my Father, let my country awake." By Guru RabindraNath Tagore (Ever since childhood this poem of RabindraNath Tagore has been synonmous to supreme level of perfection for me.Its incredibl...

      GraphQL

      What is GraphQL  API Standard invented & open-sourced by Facebook Alternative to  REST API  enables declarative data fetching  exposes single endpoint & responds to queries How it works?  Why Graphql? Improvises performance by reducing the data that is to be transferred over the internet Variety of different frontend frameworks and platforms on client-side Fast development speed & expectation for rapid feature development Why Graphql is better than REST? Flexibility & efficient  No more over /under fetching of data Over fetching : Under fetching: Insightful analytics  Schema serves as contract between client and server CORE CONCEPTS : SDL :SCHEMA DEFINITION LANGUAGE Writing Data with mutations 3 kinds of mutations creating new data updating existing data deleting existing data