requireJS worked for one plug-in, but not another

廉价感情. 提交于 2019-12-13 04:33:59

问题


I previously asked this question, and got one plug-in to work. Now, I'm trying to get another plug-in to work using the solution for the first plug-in, but that solution isn't working.

I'm trying to get this plug-in to work, but the chrome console spits out this error:

Uncaught ReferenceError: jQuery is not defined :3000/js/libs/textarea_auto_expand.js:41

My code is this:

require.config({
    paths: {
      jquery: '/js/libs/jquery/jquery-2.0.3',
     underscore: '/js/libs/underscore/underscore-min',
     backbone: '/js/libs/backbone/backbone-min',
  //text: '/js/libs/text'
 templates: '../templates'
 ,sockets: '/socket.io/socket.io' 
 ,rangyInputs: '/js/libs/rangyinputs-jquery-1.1.2'
 , textareaAutoExpand: 'js/libs/textarea_auto_expand'

},
shim: {
  'Backbone': ['Underscore', 'jQuery'],
  'sockets': {exports: 'io'}, 
  'rangyinputs-jquery': {deps: ['jquery'], exports: '$'}, 
  'textarea_auto_expand': {deps: ['jquery'], exports: '$'}

} 
});

  require(['jquery', 'router', 'libs/a_myLib/keydownHandler', 'libs/textarea_auto_expand' ], 
function($, router, keydownHandler, ta_ae){
$("body").on("keydown", "textarea", keydownHandler);
router.initialize();
$("textarea").textareaAutoExpand();
})

回答1:


The problem is that, you used path name jquery, but specified jQuery in you Backbones deps:

'Backbone': ['Underscore', 'jQuery'],

Again, here is working example of main.js

index.html

<!doctype html>
<html>
    <head></head>
    <body>
        <script data-main="main" src="require.js"></script>
    </body>
</html>

main.js

require.config({
    paths : {
        jquery : 'jquery-2.0.3',
        'rangyinputs-jquery' : 'rangyinputs-jquery-1.1.2',
        textareaAutoExpand: 'js/libs/textarea_auto_expand'
    },
    shim : {
        'rangyinputs-jquery' : {deps : ['jquery'], exports : '$'},
        'textarea_auto_expand': {deps: ['jquery'], exports: '$'}
    }
});


require(['jquery', 'rangyinputs-jquery', 'textarea_auto_expand'], function($) {
    console.log('Type of $.fn.textareaAutoExpand ' + typeof $.fn.textareaAutoExpand );
    var t = $('<textarea/>').textareaAutoExpand();
    $('body').append(t);
});


来源:https://stackoverflow.com/questions/20937259/requirejs-worked-for-one-plug-in-but-not-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!