• Mark Brenner
    0
    Example 1: Send an id as data to the server, save some data to the server, and notify the user once it's complete. If the request fails, alert the user.

    var menuId = $( "ul.nav" ).first().attr( "id" );
    var request = $.ajax({
      url: "script.php",
      type: "POST",
      data: { id : menuId },
      dataType: "html"
    });
     
    request.done(function( msg ) {
      $( "#log" ).html( msg );
    });
     
    request.fail(function( jqXHR, textStatus ) {
      alert( "Request failed: " + textStatus );
    });
    
  • Douglas Torres
    1
    Generic asynchronous cache

    It is also possible to make the code completely generic and build a cache factory that will abstract out the actual task to be performed when a key isn't in the cache yet:

    $.createCache = function( requestFunction ) {
        var cache = {};
        return function( key, callback ) {
            if ( !cache[ key ] ) {
                cache[ key ] = $.Deferred(function( defer ) {
                    requestFunction( defer, key );
                }).promise();
            }
            return cache[ key ].done( callback );
        };
    }
    
bold
italic
underline
strike
code
quote
ulist
image
url
mention
reveal
youtube
tweet
Add a Comment

Welcome to PlushForums!

You're ready to build the perfect forum for your community or organisation. Try this demo to get a feel for what your site can be like. Please bear in mind this forum is reset every day.