Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJs - Define vs Require

For modules I don't return an object I have been using require instead of define. For example say I have the following jQuery plugin (jquery.my-plugin.js):

require(['jquery'], function($) {     $.fn.myPlugin = function(options) {         ...     }; }); 

Now if I say the following in another module:

require(['jquery', 'jquery.my-plugin'], function($) {     $('#element').myPlugin(); }); 

I've found this doesn't work because myPlugin has not been registered. However if I change the require to a define within my jquery.my-plugin module then it works fine.

I'd appreciate it if someone could clear up why I have to do this. I like to understand something fully before I go ahead and use it. Thanks

like image 302
nfplee Avatar asked Jun 28 '13 13:06

nfplee


People also ask

What is the difference between require and define?

require() and define() both used to load dependencies. There is a major difference between these two method. Require(): Method is used to run immediate functionalities. define(): Method is used to define modules for use in multiple locations(reuse).

How do you define require in JS?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

Do I need 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.

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.


2 Answers

Essentially, when you use require you are saying "i want this, but i want all its dependencies too". So in the example below, we're requiring A, but require will search for all dependencies and ensure they are loaded before continuing.

require(['a'], function(a) {     // b, c, d, e will be loaded });  // File A define(['b','c','d','e'], function() {     return this; }); 

General rule of thumb is you use define when you want to define a module that will be reused by your application and you use require to simply load a dependency.

like image 150
Paul Osborne Avatar answered Sep 21 '22 23:09

Paul Osborne


Below is the code that should be inside jquery.my-plugin.js which defines a module called 'jquery.my-plugin' that can be used as a dependency elsewhere.

define(['jquery'], function($) { //jquery is a dependency to the jquery.my-plugin module     $.fn.myPlugin = function(options) { //adds a function to the *global* jQuery object, $ (global since jQuery does not follow AMD)         ...     }; }); 

Below is a section of code where you want to attach your plugin function to the global jQuery object and then use it ...

require(['jquery.my-plugin'], function() { // jquery.my-plugin is loaded which attaches the plugin to the global JQuery object as shown above, then this function fires      //the only reason $ is visible here is because it's global. If it was a module, you would need to include it as a dependency in the above require statement     $('#element').myPlugin(); //the $ refers to the global object that has the plugin attached }); 
like image 29
Niko Bellic Avatar answered Sep 19 '22 23:09

Niko Bellic