Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8353571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:18:15+00:00 2026-06-09T09:18:15+00:00

I’m building a complex web app and to keep things simple, I made various

  • 0

I’m building a complex web app and to keep things simple, I made various modules (objects) in various files. Those modules can be required on some page, and not on others.

For that reason, I’d like to avoid loading all the modules for any pages, increasing the amount of useless requests.

So far, I work like this :

  1. I include all needed libraries
  2. After that, I instantiate these librairies in jQuery(function() {}); with specifics #ids or .classes arguments for that current page

Everything works fine, but since my app is growing beyond easy, I’d like to manage my JS with RequireJS.

And that’s where things start to be a little confusing for me.

I know I can load my module when required, using require(['module1', 'module2', 'etc'], function (module1, module2, etc) {}), but how can I say :

"on this page, you load these modules, and instantiate them with those #ids and .classes"

and

"on this other page, you load only that module, with this #other-id"

?

module1 will, for example, load data from the api, and list them to a specific table given as parameter :

// in page 1
var mod1 = new Module1($('#content>table');
mod1.init(); // will load the data from the api with the url located at <table data-api-url="http://....">

// in page 2
var mod1 = new Module1($('#content .items>table'); // The table where we want the data to be populated is not at the same position!
mod1.init();

That means, depending on the page, I’ll have to load my modules differently. That’s how I don’t know how to do using RequireJs :/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:18:17+00:00Added an answer on June 9, 2026 at 9:18 am

    What you need is a javascript file for each page. That file will be responsible to execute your page-specific code.

    I’ll assume that you will use r.js to optmize and pack your code.

    We can interpret Module1, Module2 etc. as libraries, because they will be used on multiple pages. To avoid browser do one request for each library module you can include this modules on your optimized main file:

    Configure the “modules:” attribute of your build profile like this:

    ...
    modules: [
       {
           name: "main" // this is your main file
           includes: [
              "module1",
              "module2",
              "etc..."
           ]
       }
    ]
    ...
    

    By doing this you tell to requirejs something like this: Optimize my “main.js” file and include in it all its dependencies, also includes the “module1”, “module2” etc.
    You have to do this because on your main file you do not include these modules on the require()/define() call, but you still want they available in case the page-specific module needs them. You don’t have to do this for every library module you have, just for those that will be used by most of your pages.

    Then, you create a javascript file for your page to use those modules:

    define(function(require, exports, module) {
       var $ = require("jquery"),
           module1 = require("module1");
    
       var mod1 = new Module1($('#content>table'));
       mod1.init();       
    
       //other page specific-code.
    });
    

    And then on the html file:

    <script data-main="main" data-start="home" src="require.js"></script>
    

    So, when page loads, it will make a request for require.js, then another for main.js and then another for home.js and that’s all.

    I’ll put the link for the another question so this answer get some context: How to use RequireJS build profile + r.js in a multi-page project

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.