Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require.js lazy loading remote url

I have a file called moment.js on my local file system and loading it as follows with require.js works:

initialize: function() {

    require(['moment'], function(data) {
        console.log(data);
    });
}

However, if I do:

initialize: function() {

    require(['http://momentjs.com/downloads/moment.min.js'], function(data) {
        console.log(data);
    });
}

data comes back undefined. Why is this? and how do I dynamically include remote modules at runtime?

like image 398
user3585419 Avatar asked Apr 29 '14 13:04

user3585419


1 Answers

I noticed that the code you are trying to load hardcodes the module name as moment so configure RequireJS on the spot so that you can require with the same name:

initialize: function() {

    require.config({
        paths: { moment: 'http://momentjs.com/downloads/moment.min' }
    });

    require(['moment'], function(data) {
        console.log(data);
    });
}
like image 74
Louis Avatar answered Oct 18 '22 02:10

Louis