Sha256: 0a678a9a020e54ed6cff336974235971a2f6f32e97c100259aa2b245ef740875

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

/**
 *
 * Reliably ensure that the PayPal SDK is either loaded or will
 * be loading.
 *
 * @namespace WORKAREA.paypal
 */
WORKAREA.registerModule('paypal', (function () {
    'use strict';

    var handlers = [],
        attempts = 0,
        paypalLoaded = null,

        /**
         * Define a function that will be called when the paypal SDK has
         * loaded.
         *
         * @method
         * @name ready
         * @param function handler - A function to be called when PayPal
         *                           is available.
         * @memberof WORKAREA.paypal
         */
        ready = function(handler) {
            if (paypalLoaded) {
                handler();
            } else {
                handlers.push(handler);
            }
        },

        /**
         * Continuously check for whether the Paypal SDK has loaded
         * every 5 seconds, and call function handlers defined in
         * `ready()` when the `window.paypal` object is available. This
         * will only occur if the `<script>` tag containing the PayPal
         * SDK is detected to be on the page.
         *
         * @method
         * @name init
         * @memberof WORKAREA.paypal
         */
        init = function() {
            var $sdk = $('script[data-partner-attribution-id]'),
                paypalExpectedToLoad = !_.isEmpty($sdk);

            attempts += 1;
            paypalLoaded = window.paypal !== undefined;

            if (!paypalLoaded && paypalExpectedToLoad && attempts <= 20) {
                window.setTimeout(init, 2000);
            } else if (paypalLoaded) {
                _.each(handlers, function(handler) { handler(); });
                handlers = [];
            }
        };

    return {
        ready: ready,
        init: init
    };
}()));

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-paypal-3.0.6 app/assets/javascripts/workarea/storefront/paypal/modules/paypal.js
workarea-paypal-3.0.5 app/assets/javascripts/workarea/storefront/paypal/modules/paypal.js