Question

I have a problem adding the Imager.js library to require.js load, the script work perfect without require.js

Main.js

define([
        'jquery',
        'bootstrap',
        'imager',
        'video'
    ],
    function($) {
        'use strict';
        $('.dropdown-toggle').dropdown();
        $('#bvideo').magnificPopup({
            type: 'inline'
        });
        videojs('video-cdve', {
            'controls': true,
            'autoplay': false,
            'preload': 'auto',
            'width': 'auto',
            'height': 'auto'
        });
        new Imager({
            availableWidths: {
                320: '320x240',
                640: 'large',
                1024: 'large_x2',
            }
        });
    });

I obtain this error

Uncaught ReferenceError: Imager is not defined

When i define Imager

define([
        'jquery',
        'bootstrap',
        'imager',
        'video'
    ],
    function($, Imager) {
        'use strict';
        $('.dropdown-toggle').dropdown();
        $('#bvideo').magnificPopup({
            type: 'inline'
        });
        videojs('video-cdve', {
            'controls': true,
            'autoplay': false,
            'preload': 'auto',
            'width': 'auto',
            'height': 'auto'
        });
        new Imager({
            availableWidths: {
                320: '320x240',
                640: 'large',
                1024: 'large_x2',
            }
        });
    });

And now i recive this error

Uncaught TypeError: undefined is not a function

Whats happend? How i add correctly this library with require??

Was it helpful?

Solution

Your parameters are bound to modules in the same order as how you list them in the requirements so when you do this:

define([
        'jquery',
        'bootstrap',
        'imager',
        'video'
    ],
    function($, Imager) {

you are binding Imager to the value of the 'bootstrap' module. Change the order:

define([
        'jquery',
        'imager',
        'bootstrap',
        'video'
    ],
    function($, Imager) {

I also considered whether you'd need a shim for Imager but I see in their code that they detect an AMD environment and call define.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top