Se0ng11
Se0ng11

Reputation: 2333

require.config must declare in every page?

//main.js
    require.config({
        baseUrl: '../Scripts/lib/',
        paths: {
            'jquery': 'jquery-2.0.0',
        }
    });

define(['jquery', '../test1'], function ($, object) {
    $(document).ready(function () {
        $('#button').bind('click', function () {
            object.getsomething();
        });
    });
});
//

//main1.js
    require.config({
        baseUrl: '../Scripts/lib/',
        paths: {
            'jquery': 'jquery-2.0.0',
        }
    });

define(['jquery', '../test1'], function ($, object) {
    $(document).ready(function () {
        $('#button').bind('click', function () {
            object.getsomething2();
        });
    });
});
//
//test1.js
define(function () {
    return {
        getsomething: function(){
            $('#button').css({'background-color':'#000'})
        },
        getsomething2: function(){
            $('#button').css({'background-color':'#000'})
        }
    }
     //return shit;
});
//

//Default1.aspx
<script data-main="<%: ResolveUrl("~/Scripts/main.js") %>" src="<%: ResolveUrl("~/Scripts/lib/require.js") %>"></script>

//Default2.aspx
<script data-main="<%: ResolveUrl("~/Scripts/main1.js") %>" src="<%: ResolveUrl("~/Scripts/lib/require.js") %>"></script>

is it a must to declare require.config in every page that want to use the jquery? Cant I share to use the main.js in different pages? I kinda lost now, I thought it suppose to share the require.config among all the page, and each page should have their own js, and requirejs will help to put the thing that I want, as requirejs only can point to js that have require.config? search through google, still cant make up my mind on how to use on multiple page and script, need some guideline to clear out my mind

Update:1

//main.js
    require.config({
        baseUrl: '../Scripts/lib/',
        paths: {
            'jquery': 'jquery-2.0.0',
        }
    });
});
//

//Scripts/script1.js
define("main",['jquery', '../test1'], function ($, object) {
    $(document).ready(function () {
        $('#button').bind('click', function () {
            object.getsomething();
   });
});
//


//Scripts/script2.js
define(['jquery', '../test1'], function ($, object) {
    $(document).ready(function () {
        $('#button').bind('click', function () {
            object.getsomething2();
    });
});

//test1.js
define(function () {
    return {
        getsomething: function(){
            $('#button').css({'background-color':'#000'})
        },
        getsomething2: function(){
            $('#button').css({'background-color':'#000'})
        }
    }
     //return shit;
});
//

//Default1.aspx
<script data-main="<%: ResolveUrl("~/Scripts/main.js") %>" src="<%: ResolveUrl("~/Scripts/lib/require.js") %>"></script>
<script src="./Scripts/script1.js">

//Default2.aspx
<script data-main="<%: ResolveUrl("~/Scripts/main.js") %>" src="<%: ResolveUrl("~/Scripts/lib/require.js") %>"></script>
<script src="./Scripts/script2.js">

I had re go through again every single tutorial and page, and come out something like this, is it correct to code like this? but it hit error, unless i remove the external script from the source

Upvotes: 0

Views: 306

Answers (2)

Simon Smith
Simon Smith

Reputation: 8044

RequireJS typically needs a config call on each page it is used. I say typically because you can sometimes get away with the defaults but more often than not (especially with jQuery) you will need to set paths, shim etc.

Firstly, I've not encountered a case where I've needed two main.js files, can I ask why you are as your code sample shows almost identical code?

You should not be placing define() calls in your main.js. Modules should live in their own files and your main.js should require them and call any setup code.

You might wish to browse the documentation again to see what I mean.

If you need two different main files (perhaps you'd like to initialise different modules on the page) then use different JS files to do this. Such as page1.js or page2.js that require a different set of modules. Main should be used for config and loading common modules found on all pages. This answer sheds some light on that process.

Upvotes: 2

Logan
Logan

Reputation: 2505

No you do not have to declare require.config in all the pages.

Upvotes: 0

Related Questions