angular-facebook icon indicating copy to clipboard operation
angular-facebook copied to clipboard

Deferred initialization of the provider

Open k41n opened this issue 11 years ago • 21 comments
trafficstars

Hi!

Is there any way how one can init Facebook dynamically when on specific stage AppId gets known? It is being received by some query and is not defined on config phase.

So what I would like to know - is how could I provide following sequence:

  1. Angular initializes
  2. User clicks some button
  3. We receive appId which should be used (specific customer's)
  4. User gets logged in via customer's app.

Thank you in advance!

k41n avatar Mar 10 '14 10:03 k41n

This is actually fairly big problem on our end, because we have different config settings between staging/dev and production, and we are unable to set different AppId's because of this difference.

Maybe making the Facebook.init in the run instead of a config? Alternately we can do what is mentioned here (http://stackoverflow.com/a/17500683/1519631), however this forces the user to use $http (where as using a .run means the user can provide any service they want, you can probably accept/detect if the parameter is a promise, which would mean they can initialize facebook however they want)

Might do a pull request to implement this later tonight

mdedetrich avatar Sep 14 '14 06:09 mdedetrich

@Matthew, do you not have the option to define your javascript differently in different environments? Using most standard build tools you can define different values for constants in different environments.

On Sat, Sep 13, 2014 at 11:44 PM, Matthew de Detrich < [email protected]> wrote:

This is actually fairly big problem on our end, because we have different config settings between staging/dev and production, and we are unable to set different AppId's because of this difference.

Maybe making the 'Facebook.init' in the run instead of a config? Alternately we can do what is mentioned here ( http://stackoverflow.com/a/17500683/1519631), however this forces the user to use $http (where as using a .run means the user can provide any service they want, you can probably accept/detect if the parameter is a promise, which would mean they can initialize facebook however they want)

Might do a pull request to implement this later tonight

— Reply to this email directly or view it on GitHub https://github.com/Ciul/angular-facebook/issues/36#issuecomment-55517238 .

gafilson avatar Sep 15 '14 15:09 gafilson

@gafilson We do have that option, however it would significantly complicate our development/production/staging cycle, seeing as our assets are statically hosted (including our javascript) in a single location

For other libraries (such as angulartics using google analytics) we are easily able to set up the id which we get when we do a request from the server. In any case its something that should definitely be possible, and I am currently coding a solution that lets you pass as a promise so you can initialize facebook ID any way you wish

Also, according to the anuglarjs irc channel, you should not be using .config for setting up stuff like id's. Its meant to be for statically defined properties, not for ID's on libraries which change depending on deployment environment

mdedetrich avatar Sep 24 '14 06:09 mdedetrich

@Matthew, that makes sense to me. The current solution is inflexible.

W/r to your solution would the promise be accepted by the provider and then consumed by the service during the init of the run phase? That sounds ideal.

On Tue, Sep 23, 2014 at 11:54 PM, Matthew de Detrich < [email protected]> wrote:

@gafilson https://github.com/gafilson We do have that option, however it would significantly complicate our development/production/staging cycle, seeing as our assets are statically hosted (including our javascript) in a single location

For other libraries (such as angulartics using google analytics) we are easily able to set up the id which we get when we do a request from the server. In any case its something that should definitely be possible, and I am currently coding a solution that lets you pass as a promise so you can initialize facebook ID any way you wish

Also, according to the anuglarjs irc channel, you should not be using .config for setting up stuff like id's

— Reply to this email directly or view it on GitHub https://github.com/Ciul/angular-facebook/issues/36#issuecomment-56631575 .

gafilson avatar Sep 24 '14 21:09 gafilson

My solution would retain the current option of setting an appId via .config, but you can also specify the appId in the .run phase. The implementation of the function which accepts the appId in .run phase wraps the incoming paramter as a promise using $q.when. This means if you pass in a value, it will return a promise that immediately resolves the value, and if you pass in a promise, it will return the same promise unchanged

This means if you get your appId via $http, you can just pass in the promise encapsulating the $http request and it will work fine.

mdedetrich avatar Sep 25 '14 05:09 mdedetrich

Just wanna letting u know that a pitch PR would be really appreciated.

mrzmyr avatar Sep 25 '14 08:09 mrzmyr

Quite busy this week, so still working on it. I have had to rewrite almost all of the test cases, while learning how karma works, should be done during this week

mdedetrich avatar Sep 30 '14 05:09 mdedetrich

Just let me know if you need help about the karma tests. May you can provide the prototype first and we work out the tests together ?

mrzmyr avatar Sep 30 '14 05:09 mrzmyr

I'm excited about this change. Let me know also if I can help with code reviews or anything like that.

On Mon, Sep 29, 2014 at 10:51 PM, Moritz [email protected] wrote:

Just let me know if you need help about the karma tests. May you can provide the prototype first and we work out the tests together ?

— Reply to this email directly or view it on GitHub https://github.com/Ciul/angular-facebook/issues/36#issuecomment-57270028 .

gafilson avatar Oct 01 '14 03:10 gafilson

Hey guys, unfortunately I have had less time this week then originally anticipated, so I have posted what my curent implementation is. Its unfinished but the foundations are there, will have more time to look at it next week but if someone wants to help out, then that is more then welcome!

mdedetrich avatar Oct 04 '14 03:10 mdedetrich

Hey guys, i'm encountering a similar problem and am very interested in the proposed fix. Can I help at all with getting this merged in?

absolutehype avatar Jan 29 '15 14:01 absolutehype

@absolutehype there is a pull request that I have done, however its not finished yet (due to time constraints).

I suppose you can have a look at trying to finish it off, the premise is simple, config is passed through using a promise, which can mean anything (including a $http or $resource request)

Pull request is here https://github.com/Ciul/angular-facebook/issues/36

mdedetrich avatar Feb 05 '15 00:02 mdedetrich

Just letting you guys know, that I am using the pull request and its working for me, just need to update and fix the tests

As an example, this is how I initialize my Facebook SDK

  var facebookDefer;

  facebookDefer = $q.defer();

  facebookSettings.load(function(settings) {
    return facebookDefer.resolve(settings.appId);
  });

  Facebook.initAppId(facebookDefer.promise);

facebookSettings.load just loads my appId from the server (we use require.js to manage our loading). We just wrap the loading in a standard promise, and feed the promise to Facebook.initAppId. This is in the .run, instead of .config, so you can inject things like $http and $resource without any issues

You can obviously get promises from $http or $resource. You can also put in standard values and it will work fine.

mdedetrich avatar Feb 06 '15 02:02 mdedetrich

What happened with this issue? I would really love for this to be possible :)

TheWrongAlice avatar Jun 28 '15 12:06 TheWrongAlice

I am using my branch in production, and its working fine. Some tests still need to be adjusted for it to work, and I haven't had time to do that (mainly because its non trivial, my branch made angular-facebook be entirely async)

If someone wants to work on my branch, they are more then welcome to do so. Else I will have some time in a couple of weeks to try and finish it off

mdedetrich avatar Jun 29 '15 00:06 mdedetrich

+1

stewones avatar Nov 14 '15 11:11 stewones

@mdedetrich just cant make it working in production with minified js, really need a solution to load it in run phase (or controllers), thoughts?

stewones avatar Nov 14 '15 19:11 stewones

Will have a look into it in a couple of days

mdedetrich avatar Nov 14 '15 23:11 mdedetrich

+1

IRRAT avatar Nov 18 '15 18:11 IRRAT

@stewones I just had a look at the code, there is nothing indicating why it wouldn't work minified (and we use it minified in production). Can you provide any more info?

mdedetrich avatar Nov 18 '15 21:11 mdedetrich

I've been using it for half a year now without any problems :)

TheWrongAlice avatar Nov 19 '15 07:11 TheWrongAlice