Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does RequireJS work with multiple pages and partial views?

I'm looking into RequireJS but I'm uncertain about some things.

I understand how I can load all my dependencies with main.js. But do I need to add any logic to work with those dependencies in main.js?

To me, main.js seems like a document.ready state and you enter logic there when the document has loaded, right?

And for other pages and partial views, do I need to create multiple main.js or can I just reference loaded functions in dependencies from the views in a <script> for example?

like image 289
Johan Alkstål Avatar asked May 30 '12 11:05

Johan Alkstål


People also ask

What is the main purpose of the RequireJS framework?

RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.

What is Shim RequireJS?

As per RequireJS API documentation, shim lets you. Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value. - Configuring dependencies.

Should I use RequireJS?

Generally you only use RequireJS in its loading form during development. Once the site is done and ready for deployment, you minify the code. The advantage here is RequireJS knows exactly what your dependencies are, and thus can easily minify the code in the correct order.

How do I run RequireJS?

RequireJS can be initialized by passing the main configuration in the HTML template through the data-main attribute. It is used by RequireJS to know which module to load in your application. To include the Require. js file, you need to add the script tag in the html file.


2 Answers

Update - I've added an example of using RequireJS with modular HTML components. Build tool example included - https://github.com/simonsmith/modular-html-requirejs

I have also written a blog article about this - http://simonsmith.io/modular-html-components-with-requirejs/


The approach of just using main.js for everything is probably more suited to a single page application.

The way I've handled this situation is to only include common site-wide JS in the main.js file:

On every page:

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

main.js

require.config({ // config options });  require(['jquery', 'common/ajaxLoader', 'common/someOtherModule'], function($, ajax, otherModule) {     // Modules that do stuff on every page are instantiated here  }); 

page1.html

<script>     require(['scripts/page1']); </script> 

page1.js

require(['jquery', 'page1Module'], function($, module){     // page1 specific stuff here }); 

The above example is just one of a couple of ways to handle it. Note the difference between loading a plain JavaScript file and a module.

A rule of thumb I follow is to keep all reusable modules (or Classes if it makes it easier to conceptualise) inside a define with their own dependencies etc and then use require to grab those modules, use their methods or interact with them in some way.

Using this pattern will almost certainly require use of the domReady module that is a separate plugin for RequireJS. Use this instead of a ready function in jQuery for example, as it allows modules to begin downloading before the DOM is ready which reduces the wait for your code to execute.

Edit You may wish to see another example of multi-page application in the RequireJS repo

like image 160
Simon Smith Avatar answered Oct 06 '22 22:10

Simon Smith


I have recently gone through the exercise of setting up RequrieJS with automatic build optimization in an ASP.NET MVC application. There are a lot of helpful blog articles such as Simon's that are a great reference. From an ASP.NET perspective one of the most useful I found in terms of configuring the RequireJS optimizer for multi-page ASP.NET applications was Making RequireJS play nice with ASP.NET MVC.

Using the great information already out there I have put up my own ASP.NET MVC RequireJS example on GitHub. Much of what is included is similar to examples already out there, however to address the issue of partial views, and multi-page require dependencies I have taken a slightly different approach.

_Layout.cshtml

The most notable difference from existing examples is the creation of a custom RequireViewPage that exposes methods to pass configuration data to RequrieJS as well as reference page specific require dependencies.

So your _Layout.cshtml will look much like what you'd expect with:

<head>     ...     @RenderModuleConfig()     <script type="text/javascript" src="@Url.Script("vendor/require.js")" data-main="main"></script> </head> <body>     ... 

Views & Partials

To wire up views (and in my case knockout view models), an additional script fragment has been added to the bottom of _Layout.cshtml as follows

    ...     @RenderSection("scripts", required: false)     <script type="text/javascript">require(['main'], function () { require(['lib/knockout/knockout.require']); });</script> </body> 

This will ensure that for any view dependency, the main module has been loaded (assuming dependencies for main have being defined in main.js and then allows for view specific dependencies to be wired up via data attributes.

<div data-require="@MainModule"> ... </div> <div data-require="@Module("address")"> ... </div> <div data-require="view\home\index\model"> ... </div> 

For a full explaination of the design and choices, see the README on GitHub

like image 42
Chris Baxter Avatar answered Oct 06 '22 22:10

Chris Baxter