Skip to content

Instantly share code, notes, and snippets.

@bgerrissen
Created November 24, 2010 10:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgerrissen/713430 to your computer and use it in GitHub Desktop.
Save bgerrissen/713430 to your computer and use it in GitHub Desktop.
Require JS Path Expressions

Concept Path Expressions
see https://github.com/bgerrissen/modulejs for partial implementation.

Based on plugin system from RequireJS.

rules

  • Expressions are always placed behind the path.
  • Whitespace can be applied liberally.
  • Params are optional and parenthesis can be omitted.
  • An expression always starts with an exclamation mark.
  • Expression params are always enclosed in parenthesis.
  • ? Expression params cannot contain parenthesis.
  • ? Expression params cannot contain other path expressions.

implementation

Path expressions map to functions by name. The first parameter passed to a path expression function is always the module or record containing module. The second parameter passed to a path expression function is always the expression 'params' string.

Syntax:

[path]!method[(params)]

Example multiple expressions:

foo/bar!grab(bar)!clean(bar)

Example omitted params:

foo/bar!text

Example whitespace:

foo/bar ! grab ( bar , $ ) ! clean ( bar , $)

Example implementations:

!grab(globals1,global2) // grabs global objects and lifts them onto an object representing the module.
!clean(global1,global2) // deletes global objects
!final                  // omits path formatting, the path is final
!text                   // Forces require/define to treat resource as a text file
!jsonp                  // Forces require/define to treat resource as JSONP
!jsond(global)          // Combines !grab() and !clean() to grab a globally declared JSON object.
!order(#) or !order     // forces require/define to load files in order

The following examples are derived from require/define variant of Ben Gerrissen. See: https://github.com/bgerrissen/modulejs

In this require variant, records are created as soon as a new path is detected. These records contain data and settings required to load the module and are mutable at various stages. The mutability of the record system allows a lot of plugin freedom and removes the need to override or even explose core methods.

Example 1. path-expr mapping implementation:

require.plugin("grab", function (record, param) {

    var list = param.replace(/\s*/g, "").split(",")
    , i = list.length;
    
    // 'this' inside a listener always refers to global object.
    require.listen("loaded", function (e) {

        if (e.record === record) {

            e.deafen();

            if (i === 1) {

                record.module = this[ list[ 0 ] ];

            } else if (i > 1) {

                record.module = {};

                while (i--) {

                    record.module[ list[ i ] ] = this[ list[ i ] ];

                }

            }

        }

    });

});

Example 2. path-expr mapping implementation:

require.plugin("final", function (record, param) {

    record.context = ""; // no lib root
    record.extention = ""; // no .js

});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment