I'm changing a project from an "old" browser-style module structure to a "new" browser-or-server-side-javascript module structure with require.js.
On the client I'm using an offsite hosted jQuery, so I started from the example they give in the "use priority config" technique of the README:
<title>My Page</title> <script src="scripts/require.js"></script> <script> require({ baseUrl: 'scripts', paths: { jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min', jqueryui: ..., ... ... // bunch more paths here }, priority: ['jquery'] }, [ 'main' ]); </script>
This is actually working all right. But I'd like to export functionality from main to the HTML webpage itself. For instance:
<a class="button" href="#" onclick="MyApi.foo();"> <img src="foo.png" alt="foo" />Click for: <b>Foo!</b> </a>
Before fitting into the AMD module pattern, I'd exposed functionality from my various files by creating a dictionary object in the global space:
// main.js var MyApi = {}; jQuery(document).ready(function($) { // ...unexported code goes here... // export function via MyApi MyApi.foo = function() { alert("Foo!"); }; });
But I don't know what the right approach in require.js is. Is it okay to put in the HTML more require
statements inside of <script>
tags, and then name modules so that it can be used from within the webpage? Or should this always be done dynamically inside of main.js, like $('#foobutton').click(...)
?
To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.
RequireJS is a JavaScript file and module loader. It improves perceived page load times because it allows JavaScript to load in the background. In particular, it enables asynchronous JavaScript loading.
Advertisements. 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. For instance − <script data-main = "scripts/main" src = "scripts/require.js"></script>
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.
One benefit from using AMD is to drop the need for namespaces. Trying to create them again with RequireJS will go against the patterns AMD promotes.
With regard to using main.js
, this is only really appropriate when you have a single page app and all your code be reference from one place. You're free to make additional calls to require and load other modules as you need them.
Using your example from above, you could approach it this way:
foo.js
define(['jquery'], function($) { // Some set up // code here // Return module with methods return { bar: function() { } } });
page.js
require(['foo'], function(foo) { // jQuery loaded by foo module so free to use it $('.button').on('click', function(e) { foo.bar(); e.preventDefault(); }); });
Then in your page request the page.js file with require.
Using your example above:
require({ // config stuff here }, [ 'page' ]);
Or loading it in the page further down:
<script> require(['page']); </script>
Some additional points
Using the pattern above, page.js could easily require many other modules to load other page related functionality.
Where before you would attach members to your global namespace, you now split that code into separate modules that can be re-used on any page. All without reliance on a global object.
Using require this way to attach events to your DOM elements will most likely rely on the DOM Ready module provided by RequireJS
You can also set the reference on javascript's window class.
At the bottom of the application module window.MyApi = MyApi;
<a class="button" href="#" onclick="MyApi.foo();"></a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With