RequireJS – How to Load Custom JS and Dependencies

javascriptrequirejstheme

I'm struggling to understand where and how to correctly configure RequireJS to load my custom JS and it's dependencies. What I have below is working but I don't think it's set up correctly.

My theme requirejs-config.js

var config = {
    paths: {
        'myapp': '"js/myapp"',
        'owlcarousel': 'js/vendor/owl.carousel',
    },
    shim: {
        'myapp': {
            deps: ['jquery','owlcarousel']
        },
        'owlcarousel': {
            deps: ['jquery']
        },
    }
};

My custom JS myapp.js

require(['jquery','owlcarousel'], function($){

    var myCarousel = $(".carousel").owlCarousel();
    console.dir($); 
    console.log(myCarousel);

});

To get myapp.js to output as expected, I have to put the following in one of my template files but if I load it just after the body tag it gives an error, which suggest that my script it not waiting for require resources before it runs and only works by chance if it's loads later.

<script>
    require (['js/sozoapp'], function() {});
</script>

Can anyone explain what I'm missing here?

Best Answer

Can you plz change with above code,

var config = {
    paths: {
        'myapp': 'js/myapp',
        'owlcarousel': 'js/vendor/owl.carousel',
    },
    shim: {

        'owlcarousel': {
            deps: ['jquery']
        },

        'myapp': {
            deps: ['jquery','owlcarousel']
        },
    }
};
Related Topic