Quantcast
Channel: javascript – frag (frăg)
Viewing all articles
Browse latest Browse all 37

Using RequireJS sugar syntax in main.js files

$
0
0

I love the sugar API of RequireJS. It’s elegant, clean and nearly identical to CommonJS.

However, the examples often fail to demonstrate how to use it from a main.js file which does a require call, rather than a define. Doing something like this is quite useful, particularly when your config object is not a require call in itself:

(function(config, require, define){
    'use strict';

    // config in
    require.config(config);

    // main logic, wrapped in a define but working as a require
    define(function(require){
        var App = require('apps/foo/controllers/main'),
            instance = new App();
    });
}.call(this, {
    baseUrl: 'js',

    paths: {
        apps: '../bower_components/',
        react: '../bower_components/react/react'
    }
}, require, define); 
// passes local refs to closure, sets scope to global object with strict mode on.

Viewing all articles
Browse latest Browse all 37

Trending Articles