Code Monkey home page Code Monkey logo

Comments (6)

pc035860 avatar pc035860 commented on August 18, 2024

Hello @pspeter3, thank you for asking!

After looking through the code, you'll find out that the current callback function's angular context application($rootScope.$apply) is actually determined by the user.

The wrapped $FB APIs try to extract the arguments from your input, find out any is function argument, then $apply the angular context to it.

angular.forEach(args, function (arg, i) {
  /**
   * Wrap API function arg with angularjs context
   */
  if (angular.isFunction(arg)) {
    var func = arg;
    args[i] = function () {
      var funcArgs = Array.prototype.slice.call(arguments);

      if ($rootScope.$$phase) {
        // already in angularjs context
        func.apply(null, funcArgs);
      }
      else {
        // not in angularjs context
        $rootScope.$apply(function () {
          func.apply(null, funcArgs);
        });
      }
    };
  }
});

So the difficulty for returning $q promise is

If there's no callback function presented, wrapper function wouln't know where to $apply angular context, hence failing to resolve the $q promise

And I come up with a solution for that - always present the original callback function with an angular.noop(empty function) or a null value. Here's an example how it will look like after implementation:

/**
 * Modified API demo
 */
$FB.getLoginStatus(null)
.then(function (res) {
  $scope.loginStatus = res;

  return $FB.api('/me', null);
})
.then(function (res) {
  $scope.apiMe = res;
});

/**
 * Combined with $q.all
 */
$q.all([
  $FB.api('/me', null),
  $FB.getLoginStatus(null, true),
  $FB.ui(options, null)
])
.then(function (rsvList) {

  var apiMeRes = rsvList[0],
      loginStatusRes = rsvList[1],
      uiRes = rsvList[2];

});

Otherwise, I'll have to specify which argument in each method is the callback function in reference to Facebook JavaScript SDK doc.

from angular-easyfb.

pspeter3 avatar pspeter3 commented on August 18, 2024

Fair enough, do you think it's worth specifying it?

from angular-easyfb.

pc035860 avatar pc035860 commented on August 18, 2024

Sure. I'll try to implement the feature this weekend.

from angular-easyfb.

pspeter3 avatar pspeter3 commented on August 18, 2024

Thanks!

from angular-easyfb.

pc035860 avatar pc035860 commented on August 18, 2024

Hi @pspeter3 ,

I just pushed the version supports $q promise to develop branch,
please try it and let me know whether it works well or not.
And I'll merge it to master if it does work well, thanks!!

Here's an new version of demo for $q promise version
http://plnkr.co/edit/UMUtFc?p=preview

The develop version of includable scripts are available at

from angular-easyfb.

pspeter3 avatar pspeter3 commented on August 18, 2024

That looks awesome. Thank you!

from angular-easyfb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.