SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery facebook promise

    Hi all,
    what's the difference between this
    1)
    PHP Code:
     var ready = $.Deferred();
      
    window.FBReady ready.promise();
      
    window.fbAsyncInit = function() {
        
    FB.init({
          
    appId      window.FB_APP_ID// App ID
          
    channelUrl '//' window.location.host '/channel.html'// Channel File
          
    status     true// check login status
          
    cookie     true// enable cookies to allow the server to access the session
          
    xfbml      true,  // parse XFBML
          
    frictionlessRequeststrue      
        
    });

        
    // Make sure the user is logged in and redirect if needed
        
    FB.getLoginStatus(function(response) {
          if (
    response.status === 'connected') {
            
    ready.resolve(response);
          } else {
            
    ready.reject(response);
          }
        });
      };
      
      
    window.FBReady.done(function(response){
       
    //do something 
      
    }).fail(function(response){
        
    //do something 
      
    }); 
    and
    2)
    PHP Code:
    var ready = $.Deferred();
      
      
    window.fbAsyncInit = function() {
        
    FB.init({
          
    appId      window.FB_APP_ID// App ID
          
    channelUrl '//' window.location.host '/channel.html'// Channel File
          
    status     true// check login status
          
    cookie     true// enable cookies to allow the server to access the session
          
    xfbml      true,  // parse XFBML
          
    frictionlessRequeststrue      
        
    });

        
    // Make sure the user is logged in and redirect if needed
        
    FB.getLoginStatus(function(response) {
          if (
    response.status === 'connected') {
            
    ready.resolve(response);
          } else {
            
    ready.reject(response);
          }
        });
      };
      
     
    ready.done(function(response){
       
    //do something 
      
    }).fail(function(response){
        
    //do something 
      
    }); 
    imo the promise is not necessary
    but I've a doubt ^^

    Can you enlight me, please ?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Good question!

    A Deferred object is a Promise with methods that allow its owner to resolve or reject it.
    When you call the Deferred's promise() method, you create a 'pure' Promise which is identical to the Deferred, except that the resolve() and reject() methods are missing.
    This is important for purposes of encapsulation, for example if you wish to return a Promise from a function, and only allow the caller to read its state or to attach callbacks to it.

    From the documentation:
    The deferred.promise() method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request.
    So a Promise is used when it doesn't make sense for the value to be modified. For example, when jQuery makes an AJAX request it returns a Promise object.

    To bring this back to your original question, I don't think it makes any difference in your code if you attach your callbacks to the Deferred or to its Promise.
    However, based on what I have written above, I would prefer to use method 1.

    If anyone else has any opinions, I'd be glad to hear them.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •