簡體   English   中英

在RequireJS中的Require中使用Require

[英]Using Require Within Define in RequireJS

是否可以在定義塊中使用require?

我正在嘗試加載facebook JS API,但需要將其包裝在具有不同上下文的require中,因為如果Facebook被防火牆阻止,我不希望整個應用程序停止加載。

我的問題是如何從嵌套的require()調用中返回模塊。

我的代碼如下所示:

define( [ 'require' ], 
    function( require ) 
{
    var fbRequire = require( 
    { 
        context: 'fb',
        waitSeconds: 3
    } );

    fbRequire.onError = function()
    {
        console.warn( 'fbRequire.onError', arguments );
    };

    fbRequire( [ 'https://connect.facebook.net/en_US/all/debug.js' ], 
        function() 
    {
        console.log( 'facebook file now loaded' );

        // init the Facebook JS SDK
        var facebookSettings = {
            channelUrl: '//' + window.location.hostname + '/facebook-channel.html', // Channel File for x-domain communication
            status: true, 
            cookie: true, 
            xfbml: true,
            appId: '1234567890'
        };

        FB.init( facebookSettings );

        // this is the bit I'm confused about
        // how do I...
        return FB;
        // ...because it's asynchronous and needs to be returned from the define() call?
    } );
} );

這是一個Dojo項目,如果有幫助的話(我需要使用dojo / Deferred嗎?)。

我已經解決了問題,但是在Facebook被阻止時無法使Dojo加載程序不發生故障轉移。 相反,我使用了簡單的JS來加載SDK。 希望這會幫助其他人。

define( [ 'require' ], 
    function( require ) 
    {
        return {
            /**
             * Loads a facebook asynchronously using require() and a "bang" (!).
             * 
             * @example require( ['my-project/facebookLoader!'],    function( facebook ) { ... } );
             */
            load: function( store, require, callback )
            {
                // already loaded?
                if( window.FB !== undefined )
                {
                    console.log( 'facebook already loaded - using global FB object.' );
                    callback( window.FB );
                }
                else
                {
                    require( [ 'dojo/dom-construct', 
                        'dojo/_base/window', 
                        'dojo/on',
                        'dojo/_base/event' ], // remove "/debug" in live env
                    function( domConstruct, win, on, event ) 
                    {
                        // add Facebook div
                        domConstruct.create( 'div', { id:'fb-root' }, win.body(), 'first' );

                        // init the Facebook JS SDK
                        var facebookSettings = {
                            channelUrl: '//' + window.location.hostname + '/facebook-channel.html', // Channel File for x-domain communication
                            status: false, // check the login status upon init?
                            cookie: true, // set sessions cookies to allow your server to access the session?
                            xfbml: false    // parse XFBML tags on this page?
                        };

                        // app ID from the App Dashboard
                        if( window.location.hostname == 'localhost' )
                        {
                            facebookSettings.appId = '123456788'; 
                        }
                        else
                        {
                            facebookSettings.appId = '123456789'; 
                        }

                        // what do we do if Facebook is blocked or times out?
                        var loadFailed = function( errorEvent )
                        {
                            console.warn( 'Facebook failed to load. Some features will be unavailable. ' + ( errorEvent ? 'Script error.' : 'Timed out.' ) );

                            // scrap the timer (in case we got here from error event on script tag)
                            if( timerId )
                            {
                                window.clearTimeout( timerId );
                            }

                            if( errorEvent !== undefined )
                            {
                                event.stop( errorEvent );
                            }

                            window.fbAsyncInit = function(){}; // noop

                            // return fake Facebook object
                            callback( 
                            { 
                                getLoginStatus: function()
                                {
                                    return false;
                                }
                            } );

                        };

                        // give Facebook 5 seconds to load
                        var timerId = window.setTimeout( loadFailed, 5000 );

                        // hook into Facebook's load callback
                        window.fbAsyncInit = function() 
                        {
                            window.clearTimeout( timerId );
                            FB.init( facebookSettings );
                            callback(  window.FB );
                        };

                        // Load the SDK Asynchronously
                        ( function(d)
                        {
                            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                            if (d.getElementById(id)) {return;}
                            js = d.createElement('script'); js.id = id; js.async = true;
                            js.src = "//connect.facebook.net/en_US/all/debug.js"; // dev version
                            //js.src = "//connect.facebook.net/en_US/all.js"; // production version
                            on( js, 'error', loadFailed );
                            ref.parentNode.insertBefore(js, ref);
                        }( document ) );

                    } );
                }
            }
        };
    } 
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM