user1995781
user1995781

Reputation: 19463

Yepnope fallback

I am trying to use yepnope for handling fallback for public CDN.

Example code:

yepnope([
{
    load: [
        'http://tinymce.cachefly.net/4.0/tinymce.min.js'
    ],
    complete: function(){
        if (typeof(tinymce) === 'undefined') {
            yepnope('/js/tinymce.min.js');
        };
        tinymce.init({selector:'textarea'});
    }
}
]);

The code is basically working. When the public CDN fails, it will load the local script. But the problem is, it does not wait for the local script to load and continue with execution. In this example code, it runs tinymce.init({selector:'textarea'}); before local tinymce script is loaded.

How can I solve it? Thank you.

Upvotes: 0

Views: 173

Answers (1)

Barmar
Barmar

Reputation: 781096

yepnope is asynchronous, anything that has to wait for loading has to be done in the complete option.

yepnope([
    {
        load: [
            'http://tinymce.cachefly.net/4.0/tinymce.min.js'
        ],
        complete: function(){
            if (typeof(tinymce) === 'undefined') {
                yepnope([
                    {
                        load: ['/js/tinymce.min.js'],
                        complete: function() {
                            tinymce.init({selector: 'textarea'});
                        }
                    }
                ]);
            } else {
                tinymce.init({selector:'textarea'});
            }
        }
    }
]);

Upvotes: 3

Related Questions