Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require using AMD pattern gives error for jQuery UI events

In my code test.js is dependent on jquery-ui which does not uses require AMD pattern and test.spec.js dependent on jquery-ui, test.js which uses AMD pattern. Can we load dependency of jquery-ui in test.js dynamically when running test.spec.js.

require.config({

    baseUrl: '/demo',

    paths: {
        'jquery': '../library/jquery-1.11.1',
        'jquery-ui': '../library/jquery-ui-1.11.4'
    },
    shim: {
        'jquery': {
            exports: 'jQuery'
        },
        'jquery-ui': {
            deps: ['jquery']
        },
        'library/src/js/test': {
            deps: ['library/jquery-1.11.1', 'library/jquery-ui-1.11.4', '../js/collapse'],
            exports: 'Test'
        }
    },
    callback: window.__karma__.start
});

In test.js "draggable" of jquery-ui draggable event is written. after evaluating $('#panelId').draggable({revert: true}); got error

"TypeError: 'undefined' is not a function (evaluating '$('#panelId').draggable({revert: true})')"

How to load jquery-ui for test.js in require.config. As i am using this to run my jasmine test cases. In real environment it is working as expected, but in jasmine test case not able to find jquery-ui event. test.js is not using require.js, but test.spec.js uses the require AMD pattern.

in test.spec.js code after executing this got error of jquery-ui draggable undefined

define(['jquery-ui','library/src/js/test'], function ($) {

});

I am able to access jquery ui in test.spec.js using $, not in test.js where jquery-ui event is written as test.js does not uses AMD require pattern. Don't know what is missing. any help will be appriciated... :)

like image 698
Rahul Rajput Avatar asked Aug 11 '15 11:08

Rahul Rajput


People also ask

How to avoid jQuery conflict?

Thus, if you are using another JavaScript library that uses the $ variable, you can run into conflicts with jQuery. In order to avoid these conflicts, you need to put jQuery in no-conflict mode immediately after it is loaded onto the page and before you attempt to use jQuery in your page.

What is the jQuery method for attaching a custom event to an element?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.

What is a jQuery ui widget?

a jQuery UI widget is a specialized jQuery plug-in. Using plug-in, we can apply behaviours to the elements. However, plug-ins lack some built-in capabilities, such as a way to associate data with its elements, expose methods, merge options with defaults, and control the plug-in's lifetime.


1 Answers

Try this updated require js config

Use "library/src/js/test" to load your test

require.config({

    baseUrl: '/demo',

    paths: {

        'jquery': '../library/jquery-1.11.1', // assuming this is a correct path
        'jquery-ui': '../library/jquery-ui-1.11.4'  // assuming this is a correct path
    },
    shim: {
        'jquery': {
            exports: 'jQuery'
        },
        'jquery-ui': {
            deps: ['jquery']
        },
        'library/src/js/test': {
            deps: ['jquery', 'jquery-ui', '../js/collapse'], // changes done here
            exports: 'Test'
        }
    },
    callback: window.__karma__.start
});
like image 139
Kushal Avatar answered Oct 22 '22 17:10

Kushal