Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$q promises for async calls #1

Closed
pspeter3 opened this issue Sep 5, 2013 · 6 comments
Closed

$q promises for async calls #1

pspeter3 opened this issue Sep 5, 2013 · 6 comments

Comments

@pspeter3
Copy link

pspeter3 commented Sep 5, 2013

Looking through the code, I'm trying to understand how difficult it would be to wrap the async calls to facebook with $q and tie in the events to the angular event system. Thoughts?

@pc035860
Copy link
Owner

pc035860 commented Sep 5, 2013

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.

@pspeter3
Copy link
Author

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

@pc035860
Copy link
Owner

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

@pspeter3
Copy link
Author

Thanks!

@pc035860
Copy link
Owner

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

@pc035860 pc035860 reopened this Sep 15, 2013
@pspeter3
Copy link
Author

That looks awesome. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants