Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

9/17/2012

Require.js

Recently I discovered require.js, which is Javascript file and module loader.

Here is how you can import jquery module (which is jqeury library itself):
First download require.js  and place it in the root folder of your project. requirejs

Then create script tag in the head section of html document:


Where, main is the name of js file which had to be created. Choose your own, descriptive name.

Next, open main.js file, and add the following content inside:
define(["jquery-1.8.1"],function (jquery){
 // all your jquery code goes  here
})
Note: Trying to add $ as a function parameter leads to undefined error in my case, so I use jquery (that's how jquery handles AMD modules )

Now you can use jquery library, and let requirejs manage your modules. You can also remove path to jquery library in html document, because requirejs will find it.

A module can bi ordinary javascript function, so you can experiment with importing.

Here is a tutorial by the creator of requirejs:
http://www.youtube.com/watch?v=VGlDR1QiV3A

No comments:

Post a Comment