Skip to content

Instantly share code, notes, and snippets.

@devniel
Forked from ruiwen/fb-angular.js
Last active July 29, 2023 04:54
Show Gist options
  • Save devniel/5846169 to your computer and use it in GitHub Desktop.
Save devniel/5846169 to your computer and use it in GitHub Desktop.

Revisions

  1. devniel renamed this gist Jun 23, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. devniel revised this gist Jun 23, 2013. 2 changed files with 55 additions and 7 deletions.
    31 changes: 31 additions & 0 deletions UserCtrl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    function UserCtrl(
    $scope,
    $FB) {

    $scope.$watch(function() {
    return $FB.isLoaded()
    },function(value){

    console.log("VALUE",value);
    // It needs authentication, this won't work.
    if(value){
    $scope.facebook_friends = $FB.api('/me/friends', function(response) {
    $scope.facebook_friends = response.data;
    });
    }
    },true);


    $scope.$watch(function() {
    return $FB.isAuthenticated()
    },function(value){

    console.log("VALUE isAuthenticated",value);
    // YEP, this will work.
    if(value){
    $scope.facebook_friends = $FB.api('/me/friends', function(response) {
    $scope.facebook_friends = response.data;
    console.log("FRIENDS",response);
    });
    }
    },true);
    31 changes: 24 additions & 7 deletions fb-angular.js
    Original file line number Diff line number Diff line change
    @@ -42,29 +42,46 @@ angular.module('facebook', [])
    .factory('$FB', ['$rootScope', function($rootScope) {

    var fbLoaded = false;

    // Our own customisations
    var _fb = {
    loaded: fbLoaded,
    isLoaded : function(){
    return this.loaded;
    },
    authenticated : false,
    isAuthenticated : function(){
    return this.authenticated;
    },
    _init: function(params) {

    self = this;

    if(window.FB) {

    // FIXME: Ugly hack to maintain both window.FB
    // and our AngularJS-wrapped $FB with our customisations
    angular.extend(window.FB, _fb);
    angular.extend(_fb, window.FB);

    angular.extend(window.FB, this);
    angular.extend(this, window.FB);
    // Set the flag
    _fb.loaded = true;

    this.loaded = true;
    // Initialise FB SDK
    window.FB.init(params);

    window.FB.Event.subscribe('auth.authResponseChange', function(response) {
    if (response.status === 'connected') {
    self.authenticated = true;
    }
    });

    if(!$rootScope.$$phase) {
    $rootScope.$apply();
    }
    }
    }
    }

    return _fb;
    }]);
  3. @ruiwen ruiwen revised this gist May 5, 2013. 1 changed file with 69 additions and 56 deletions.
    125 changes: 69 additions & 56 deletions fb-angular.js
    Original file line number Diff line number Diff line change
    @@ -1,57 +1,70 @@
    // Facebook SDK
    angular.module('facebook', []).
    directive('fb', ['$FB', function($FB) {
    return {
    restrict: "E",
    replace: true,
    template: "<div id='fb-root'></div>",
    compile: function(tElem, tAttrs) {
    return {
    post: function(scope, iElem, iAttrs, controller) {
    var fbAppId = iAttrs.appId || '';
    // Setup the post-load callback
    window.fbAsyncInit = function() {
    $FB._init();
    if('fbInit' in iAttrs) {
    iAttrs.fbInit();
    }
    };

    (function(d, s, id, fbAppId) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js#appId=" + fbAppId;
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk', fbAppId));
    }
    }
    }
    };
    }]).
    factory('$FB', ['$rootScope', function($rootScope) {

    var fbLoaded = false;

    // Our own customisations
    var _fb = {
    loaded: fbLoaded,
    _init: function() {
    if(window.FB) {
    // FIXME: Ugly hack to maintain both window.FB
    // and our AngularJS-wrapped $FB with our customisations
    angular.extend(window.FB, _fb);
    angular.extend(_fb, window.FB);

    // Set the flag
    _fb.loaded = true;

    if(!$rootScope.$$phase) {
    $rootScope.$apply();
    }
    }
    }
    }

    return _fb;
    }]);
    angular.module('facebook', [])
    .directive('fb', ['$FB', function($FB) {
    return {
    restrict: "E",
    replace: true,
    template: "<div id='fb-root'></div>",
    compile: function(tElem, tAttrs) {
    return {
    post: function(scope, iElem, iAttrs, controller) {
    var fbAppId = iAttrs.appId || '';

    var fb_params = {
    appId: iAttrs.appId || "",
    cookie: iAttrs.cookie || true,
    status: iAttrs.status || true,
    xfbml: iAttrs.xfbml || true
    };

    // Setup the post-load callback
    window.fbAsyncInit = function() {
    $FB._init(fb_params);

    if('fbInit' in iAttrs) {
    iAttrs.fbInit();
    }
    };

    (function(d, s, id, fbAppId) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk', fbAppId));
    }
    }
    }
    };
    }])

    .factory('$FB', ['$rootScope', function($rootScope) {

    var fbLoaded = false;

    // Our own customisations
    var _fb = {
    loaded: fbLoaded,
    _init: function(params) {
    if(window.FB) {
    // FIXME: Ugly hack to maintain both window.FB
    // and our AngularJS-wrapped $FB with our customisations
    angular.extend(window.FB, _fb);
    angular.extend(_fb, window.FB);

    // Set the flag
    _fb.loaded = true;

    // Initialise FB SDK
    window.FB.init(params);

    if(!$rootScope.$$phase) {
    $rootScope.$apply();
    }
    }
    }
    }

    return _fb;
    }]);
  4. @ruiwen ruiwen revised this gist Feb 6, 2013. 1 changed file with 26 additions and 27 deletions.
    53 changes: 26 additions & 27 deletions fb-angular.js
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,32 @@
    // Facebook SDK
    angular.module('facebook', []).
    directive('fb', ['$FB', function($FB) {
    return {
    restrict: "E",
    replace: true,
    template: "<div id='fb-root'></div>",
    compile: function(tElem, tAttrs) {
    return {
    post: function(scope, iElem, iAttrs, controller) {
    var fbAppId = iAttrs.appId || '';
    // Setup the post-load callback
    window.fbAsyncInit = function() {
    $FB._init();

    if('fbInit' in iAttrs) {
    iAttrs.fbInit();
    }
    };

    (function(d, s, id, fbAppId) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js#appId=" + fbAppId;
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk', fbAppId));
    }
    }
    }
    return {
    restrict: "E",
    replace: true,
    template: "<div id='fb-root'></div>",
    compile: function(tElem, tAttrs) {
    return {
    post: function(scope, iElem, iAttrs, controller) {
    var fbAppId = iAttrs.appId || '';
    // Setup the post-load callback
    window.fbAsyncInit = function() {
    $FB._init();
    if('fbInit' in iAttrs) {
    iAttrs.fbInit();
    }
    };

    (function(d, s, id, fbAppId) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js#appId=" + fbAppId;
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk', fbAppId));
    }
    }
    }
    };
    }]).
    factory('$FB', ['$rootScope', function($rootScope) {
  5. @ruiwen ruiwen revised this gist Feb 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fb-angular.js
    Original file line number Diff line number Diff line change
    @@ -53,6 +53,6 @@ angular.module('facebook', []).
    }
    }
    }

    return _fb;
    }]);
  6. @ruiwen ruiwen revised this gist Feb 6, 2013. 1 changed file with 11 additions and 12 deletions.
    23 changes: 11 additions & 12 deletions fb-angular.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,17 @@
    // Facebook SDK
    angular.module('facebook', []).
    directive('fb', ['$FB', function($FB) {
    return {
    restrict: "E",
    replace: true,
    template: "<div id='fb-root'></div>",
    compile: function(tElem, tAttrs) {
    return {
    post: function(scope, iElem, iAttrs, controller) {
    var fbAppId = iAttrs.appId || '';

    // Setup the post-load callback
    window.fbAsyncInit = function() {
    $FB._init();
    return {
    restrict: "E",
    replace: true,
    template: "<div id='fb-root'></div>",
    compile: function(tElem, tAttrs) {
    return {
    post: function(scope, iElem, iAttrs, controller) {
    var fbAppId = iAttrs.appId || '';
    // Setup the post-load callback
    window.fbAsyncInit = function() {
    $FB._init();

    if('fbInit' in iAttrs) {
    iAttrs.fbInit();
  7. @ruiwen ruiwen created this gist Feb 6, 2013.
    59 changes: 59 additions & 0 deletions fb-angular.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // Facebook SDK
    angular.module('facebook', []).
    directive('fb', ['$FB', function($FB) {
    return {
    restrict: "E",
    replace: true,
    template: "<div id='fb-root'></div>",
    compile: function(tElem, tAttrs) {
    return {
    post: function(scope, iElem, iAttrs, controller) {
    var fbAppId = iAttrs.appId || '';

    // Setup the post-load callback
    window.fbAsyncInit = function() {
    $FB._init();

    if('fbInit' in iAttrs) {
    iAttrs.fbInit();
    }
    };

    (function(d, s, id, fbAppId) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js#appId=" + fbAppId;
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk', fbAppId));
    }
    }
    }
    };
    }]).
    factory('$FB', ['$rootScope', function($rootScope) {

    var fbLoaded = false;

    // Our own customisations
    var _fb = {
    loaded: fbLoaded,
    _init: function() {
    if(window.FB) {
    // FIXME: Ugly hack to maintain both window.FB
    // and our AngularJS-wrapped $FB with our customisations
    angular.extend(window.FB, _fb);
    angular.extend(_fb, window.FB);

    // Set the flag
    _fb.loaded = true;

    if(!$rootScope.$$phase) {
    $rootScope.$apply();
    }
    }
    }
    }

    return _fb;
    }]);
    8 changes: 8 additions & 0 deletions fb.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <!DOCTYPE html>
    <html ng-app='app'>
    <head></head>
    <body>
    <fb app-id='123123'></fb>
    </body>

    </html>