Savvy coders are increasingly making use of modular programming techniques — breaking projects into more readily manageable blocks of code that boost their reusability while making maintenance and updates easier. While these are great benefits, being able to manage the dependencies between these modules can become a problematic process.
Consider the loading of JavaScript files, for example.
Many of today’s applications make use of a range of JavaScript files, often loaded individually using a separate <script> tag for each occurrence. This hurts performance and can also cause script conflicts when files that are dependent on other files are called in the wrong order.
An easy example of this can be found in jQuery plugins, all of which are dependent on the base jQuery library, which must be loaded by your page before any of its plugins.
Enter RequireJS (www.requirejs.org), a JavaScript file and module loader designed to eliminate jQuery loading errors and other scripting conflicts, while improving the speed and quality of a website’s coding.
Optimized for in-browser use, RequireJS is compatible with Chrome 3+, Firefox 2+, IE 6+, Opera 10+ and Safari 3.2+, and can also be used in other JavaScript environments, such as Node or Rhino.
Although RequireJS doesn’t require jQuery, it’s often used alongside the technology, and demands some special considerations in order to make everything smooth and easy.
“While RequireJS loads jQuery just like any other dependency, jQuery’s wide use and extensive plugin ecosystem mean you’ll likely have other scripts in your project that also depend on jQuery,” a script spokesperson states. “You might approach your jQuery RequireJS configuration differently depending on whether you are starting a new project or whether you are adapting existing code.”
One consideration is the use of global variables, as jQuery will register itself as the global variables “$” and “jQuery,” even when it detects AMD/RequireJS.
“The AMD approach advises against the use of global functions, but the decision to turn off these jQuery globals hinges on whether you have non-AMD code that depends on them,” the spokesperson explains. “JQuery has a noConflict function that supports releasing control of the global variables and this can be automated in the require.config.”
Another consideration is module names, since jQuery defines a named AMD module “jquery” (all lower case) when it detects AMD/RequireJS, so the publisher recommends that to reduce the confusion, developers should use “jquery” as the module name in their require.config.
For example, in the require.config below, the module ID is shown on the left (jquery), while the path to the jQuery file is on the right side (jquery-1.9.0), relative to the baseUrl — in other words, the function is calling jQuery 1.9.0, located at js/lib/jquery-1.9.0.js, relative to the HTML page. Note that the path does not include the “.js” file extension:
require.config({
baseUrl: ‘js/lib’,
paths: {
jquery: ‘jquery-1.9.0’
}
});
"The other (recommended) solution is to just name the file ‘jquery.js’ and place it in the baseUrl directory. Then the above paths entry is not needed,” the spokesperson added.
“You can avoid lots of configuration lines by placing the files according to the default ID-to-path convention of baseUrl + moduleID + ‘.js’.”
The RequireJS website offers a wide range of coding examples, such as illustrating how to set the baseUrl to be the directory for third-party, library code, using one extra path config in the app code. Other instances involve using a shim configuration, as well as how to modify the plugins to be wrapped in a define() instead, for further flexibility.
Other examples show how to set the path of jQuery to point to a Google-hosted CDN, which the company says, benefits users who might already have the file in the browser cache and so won’t have to download it again. One factor to note when loading an asset from a CDN is that all plugins needing that asset as a dependency, have to call define().
It will take some time to get a grasp on the flexibility, power and value of RequireJS, but if your sites make heavy use of JavaScript and jQuery, then RequireJS is a winner.
Dev Depot: RequireJS, Harmonizing Asset Allocation
Copyright © 2024 Adnet Media. All Rights Reserved. XBIZ is a trademark of Adnet Media.
Reproduction in whole or in part in any form or medium without express written permission is prohibited.
More Articles
VerifyMy Seeks to Provide Frictionless Online Safety, Compliance Solutions
Before founding VerifyMy, Ryan Shaw was simply looking for an age verification solution for his previous business. The ones he found, however, were too expensive, too difficult to integrate with, or failed to take into account the needs of either the businesses implementing them or the end users who would be required to interact with them.
How Adult Website Operators Can Cash in on the 'Interchange' Class Action
The Payment Card Interchange Fee Settlement resulted from a landmark antitrust lawsuit involving Visa, Mastercard and several major banks. The case centered around the interchange fees charged to merchants for processing credit and debit card transactions. These fees are set by card networks and are paid by merchants to the banks that issue the cards.
It's Time to Rock the Vote and Make Your Voice Heard
When I worked to defeat California’s Proposition 60 in 2016, our opposition campaign was outspent nearly 10 to 1. Nevertheless, our community came together and garnered enough support and awareness to defeat that harmful, misguided piece of proposed legislation — by more than a million votes.
Staying Compliant to Avoid the Takedown Shakedown
Dealing with complaints is an everyday part of doing business — and a crucial one, since not dealing with them properly can haunt your business in multiple ways. Card brand regulations require every merchant doing business online to have in place a complaint process for reporting content that may be illegal or that violates the card brand rules.
WIA Profile: Patricia Ucros
Born in Bogota, Colombia, Ucros graduated from college with a degree in education. She spent three years teaching third grade, which she enjoyed a lot, before heeding her father’s advice and moving to South Florida.
Creating Payment Redundancies to Maximize Payout Uptime
During the global CrowdStrike outage that took place toward the end of July, a flawed software update brought air travel and electronic commerce to a grinding halt worldwide. This dramatically underscores the importance of having a backup plan in place for critical infrastructure.
The Need for Minimal Friction in Age Verification Technology
In the adult sector, robust age assurance, comprised of age verification and age estimation methods, is critical to ensuring legal compliance with ever-evolving regulations, safeguarding minors from inappropriate content and protecting the privacy of adults wishing to view adult content.
Account-to-Account Payments: The New Banking Disruptor?
So much of our industry relies upon Visa and Mastercard to support consumer payments — and with that reliance comes increased scrutiny by both brands. From a compliance perspective, the bar keeps getting raised until it feels like we end up spending half our time making sure we are compliant rather than growing our business.
WIA Profile: Samantha Beatrice
Beatrice credits the sex positivity of Montreal for ultimately inspiring her to pursue work in adult entertainment. She had many friends working in the industry, from sex workers to production teams, so it felt like a natural fit and offered an opportunity to apply her marketing and social media savvy to support people she truly believes in and wants to see succeed.
Understanding the Latest Server Processors
Over the last decade, we mostly stopped talking about CPU performance. Recently, however, there has been a seismic and exciting change in the CPU landscape, due to innovation by a chip company called Advanced Micro Devices (AMD).