最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Requirejs configuration in different file - Stack Overflow

programmeradmin1浏览0评论

I am using requirejs. My main.js content is like following.

requirejs.config({
    async: true,
    parseOnLoad: true,
    packages: [],
    paths: {
        jquery: '.9.1/jquery.min'
    }
});

require(["login"], function (loginService) {

    loginService.login('validUser');

});

Now, my config elements are little. But later, I will add packages, paths and others, so the require.config lines will increase.

  1. I wanna separate require.config as a different file and use it?
  2. If jquery load delays, does the error occurs? My other javascript files are using it.

I am using requirejs. My main.js content is like following.

requirejs.config({
    async: true,
    parseOnLoad: true,
    packages: [],
    paths: {
        jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min'
    }
});

require(["login"], function (loginService) {

    loginService.login('validUser');

});

Now, my config elements are little. But later, I will add packages, paths and others, so the require.config lines will increase.

  1. I wanna separate require.config as a different file and use it?
  2. If jquery load delays, does the error occurs? My other javascript files are using it.
Share Improve this question asked Sep 6, 2013 at 8:40 bartelomabarteloma 6,86516 gold badges93 silver badges205 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 14

Yes you can, require your config before you require anything else, like this:

config example:

require.config({
    baseUrl: '/Public/js',
    paths: {
        jquery: '../../Scripts/jquery-1.10.2.min',
        jqueryui: '../../Scripts/jquery-ui-1.10.2.min',
    },
    shim: {
        jqueryui: {
            deps: ['jquery']
        },
    }
    waitSeconds: 3
});

and, then I load it:

require(['/Public/js/config.js'], function() {
    require(['home/index'], function() {                
    });
});

Just remember that you reference the config.js by path in the first require-statement because require.js can not resolve by baseUrl since it has not been loaded. When you get to the inner require()-statement, its loaded and you can reference dependencies relative to baseUrl.

  1. You can put the config into a separate JS file, that's not a problem. Just make sure that file is loaded prior to the require() call in your main code.

  2. If you're using jQuery for other scripts that are not loaded via requireJS, you will get errors if they happen to load sooner than jQuery. What you need to do is convert all those static files into requireJS modules and load them all via requireJS. By using a define() function in each of the modules, you can set up dependencies, so all modules will wait for jQuery to load prior to executing their own code.

This is an example of a multipage requirejs based project where the requirejs.config call is in a separate file

https://github.com/requirejs/example-multipage/tree/master/www

发布评论

评论列表(0)

  1. 暂无评论