0

sorry if I am repeating this question, but I am quite new in requireJS and I do not fully understand the way it works.

I am trying to implement amazon-like navigation with diagonal movement over elements. I found a jQuery plugin (https://github.com/kamens/jQuery-menu-aim) for that but I want to implement it with help of requireJS.

In common way you just include all necessary scripts with <script> tag (Jquery and the plugin). Then you find your navigation and assign some functions on activating/ deactivating submenu. Like example:

          <script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
            <script src="../jquery.menu-aim.js" type="text/javascript"></script>
            <script src="js/bootstrap.min.js" type="text/javascript"></script>
            <script>
                var $menu = $(".dropdown-menu");
                // jQuery-menu-aim: <meaningful part of the example>
                 // Hook up events to be fired on menu row activation.
               $menu.menuAim({
                    activate: activateSubmenu,
                    deactivate: deactivateSubmenu
                });

           function activateSubmenu(row) {//do something}
           function deactivateSubmenu(row) {//do someting else)
         </script>

In requireJS way. In the navigation.js file i required menu-aim plugin in the beginning and tried to implement it like this:

 var menuAim = require('lib/menu-aim');
 // some code
 var $menuT = $('#nav-main-deep');
 $menuT.menuAim({
        activate: activateSubmenu,
        deactivate: deactivateSubmenu

  });
   function activateSubmenu(){
        console.log('test1');
    }

    function deactivateSubmenu() {
        console.log('test2');
    }

And i put whole plugin menu-aim.js between:

define(function () {
//whole menu-aim plugin
});

Probably, this is not the way how to run requireJS plugin, cause its doing nothing (i tried to do some console logs). I tried to look at requireJS doc but I still dont understand how to do it right.. Will be very thankfull for advices..

4

1 回答 1

2

从 GitHub 代码看来,它似乎jQuery-menu-aim没有使用通用模块定义,因此需要您的 RequireJS 配置提供一些帮助。

Ashim可以帮助 RequireJS 对依赖项进行排序,因为这是一个 jQuery 插件,并且必须首先加载 jQuery 并将其传递给它。单击此处了解有关 RequireJS 垫片的更多信息

将此代码添加到您的 RequireJS 配置文件

requirejs.config({
  shim: {
    'menu-aim': {
        deps: ['jquery']
    }
  }
});

当你打电话时

require('lib/menu-aim', function() { 
  // put your menu code here 
});

jQuery 和菜单插件将被加载。

于 2018-02-27T04:03:13.083 回答