Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using require.js with FB SDK

I would like to load FB SDK using require.js.

my test case is something like this:

test.js:

require([        
     'libs/facebook/fb'
     ], function(FB){
     FB.api("/me", function(){});
));

I would like to have test.js run only after FB SDK is loaded, and have FB ready for it.

Any thoughts on how this can be achieved? what should my wrapper (libs/facebook/fb.js) have?

like image 941
Erez Rosovsky Avatar asked Aug 07 '12 13:08

Erez Rosovsky


1 Answers

It doesn't seem like the FB API is an AMD module, so it doesn't define itself in a manner to which RequireJS is accustomed to. You will need to shim the FB API using require.config. I'm assuming test.js is the script you have provided as the data-main value for RequireJS.

require.config({
    shim: {
        'facebook' : {
            exports: 'FB'
        }
    },

    paths: {
        'facebook' : 'libs/facebook/fb'
    }
});

require(['facebook'], function(FB){
    FB.api('/me', function(){});
});
like image 52
Dzulqarnain Nasir Avatar answered Nov 15 '22 15:11

Dzulqarnain Nasir