Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access FB global from Angular2 service?

I'm trying to use FB SDK from an Angular2 project created with the new angular CLI. I created the following service to initialize the Facebook library, added fbsdk typings and everything compiles but I cant access the FB global variable from anywhere in the project but the "winwow.fbAsyncInit" function. I can check FB object from console perfectly, but cant access it from NG2 code. ¿Any idea of where the problem might be?

import { Injectable, OnInit } from '@angular/core'
import { FACEBOOK_APPID } from '../constants'

@Injectable()
export class fbService implements OnInit{
  constructor(){}
  ngOnInit(){
    if (!window.fbAsyncInit) {
        window.fbAsyncInit = function () {
            FB.init({
                appId: FACEBOOK_APPID,
                xfbml: false,
                version: 'v2.6'
            });
            FB.api("/me",null,()=>{})
            console.log(FB)
        };
    }
    this.initFB()           
}
initFB(){
  var js, id = 'facebook-jssdk', ref = document.getElementsByTagName('script')[0];
    if (document.getElementById(id)) {
        return;
    }
    js = document.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    ref.parentNode.insertBefore(js, ref);
}
getPost(page:string){
     FB.api(`/${page}/feed?limit=1`,null,(response)=>{console.log(response)})
}
}
like image 438
Perseoh Avatar asked Oct 31 '22 03:10

Perseoh


1 Answers

The key is in the call to this.zone.run(callback) to wrap the callback and reinsert it in the Angular 2 lifecycle.

I initialize the SDK in the service constructor, which may introduce some delay compared to loading it right away. Haven't found a workaround yet.

I still haven't decided if I'll provide an accessor to FB once initialized or wrapper methods, I'll leave that to you.

@Injectable()
export class FbsdkService {
    public ready = new BehaviorSubject<boolean>(false);

    constructor(
        private zone: NgZone) {
        this.loadSdkAsync(() => {
            FB.init({
                appId: "XXXXXXXXXXX",
                status: false,
                xfbml: false,
                version: "v2.7"
            });
            this.ready.next(true);
        });
    }

    loadSdkAsync(callback: () => void) {
        window.fbAsyncInit = () => this.zone.run(callback);
        // Load the Facebook SDK asynchronously
        const s = "script";
        const id = "facebook-jssdk";
        var js, fjs = document.getElementsByTagName(s)[0];
        if (document.getElementById(id)) return;
        js = document.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs)
    }
}
like image 74
Martín Coll Avatar answered Nov 15 '22 07:11

Martín Coll