-1

High-Level Problem:

  • I'd like to know if the current user is logged into Google Plus.

Partial Solution:

Complication:

Specific Problem:

  • I'd like to attach a callback method to the onload parameter, but the asynchronous JavaScript fetch of https://apis.google.com/js/client.js is occurring inside of AngularJS's run method.

-> Initialization of module:

.run([function() {  
  var po = document.createElement('script');  
  po.type = 'text/javascript';  
  po.async = true;  
  po.src = 'https://apis.google.com/js/client.js';  
  var s = document.getElementsByTagName('script')[0];  
  s.parentNode.insertBefore(po, s);  
}]);

Although Google recommends that you fetch https://apis.google.com/js/client.js asynchronously, I'm not wedded to it. But I would like to keep most of that Google+ integration code inside of either an AngularJS Provider or an AngularJS service.

Any help would be appreciated.

Community
  • 1
  • 1
Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • inject the provider in run and resolve a `$q` promise within the google callback that gets return from one of your provider methods – charlietfl Apr 28 '15 at 17:15
  • @charlietfl: It sounds like you're on to something, but I can't quite understand the code that you're prescribing. I hope that either you or someone else can elaborate further. – Jim G. Apr 28 '15 at 17:26

1 Answers1

0

I don't see what's wrong with using the onload method.

.run(['$rootScope', function($rootScope) {  
    var po = document.createElement('script');  
    po.type = 'text/javascript';  
    po.async = true;  
    po.src = 'https://apis.google.com/js/client.js';  
    po.onload = function(){
        //it's loaded. Inject a service in your run and you can do stuff here
        $rootScope.$apply(); //assuming your callback needs to do something within angular
    };
    var s = document.getElementsByTagName('script')[0];  
    s.parentNode.insertBefore(po, s);
}]);
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
  • I'd actually like to call methods inside of the same module. How can I access those? It seems like it wouldn't make sense to inject the same service attached to the `run` method. – Jim G. Apr 28 '15 at 18:34
  • 1
    onload isn't an option because even though the "script" is loaded that doesn't mean that google is done setting up the required global variable objects, therefore we require google to tell us when it is done... – user3338098 Apr 29 '15 at 17:43
  • so instead `function signinCallback() {$rootScope.$apply();}` – user3338098 Oct 28 '15 at 14:29