cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

Config option for predefined modules #105

Open gamtiq opened 11 years ago

gamtiq commented 11 years ago

Hello John,

It would be useful to have some configuration option that allows describing several predefined modules (i.e. named modules/objects that have created before curl loading). So that instead of

...
<script>
    curl = {baseUrl: ...};
</script>
<script src="path/to/curl.js"></script>
<script>
    define("m1", obj1);
    define("m2", function() { return modDef; });
    define("m1/m2", obj1.obj2);
</script>
...

it could be possible to write something like the following

...
<script>
    curl = {
        predefined: {
            m1: obj1,
            m2: function() { return modDef; },
            "m1/m2": obj1.obj2
        },
        baseUrl: ...
    };
</script>
<script src="path/to/curl.js"></script>
...

Best regards, Denis

unscriptable commented 11 years ago

Interesting iea. Where do these modules come from? Can you help me understand some use cases?

This might work (depending on your use case):

<script src="path/to/curl.js"></script>
<script>
    curl({ baseUrl: '...', etc: '...' });
    define("m1", obj1);
    define("m2", function() { return modDef; });
    define("m1/m2", obj1.obj2);
</script>
gamtiq commented 11 years ago

John,

Actually your code is similar to my snippet 1. I use curl along with my own framework. The framework’s loader loads some framework files before curl. Those files contain "plain js" (not AMD) organized by namespaces. As a result I have an objects hierarchy. Some objects from this hierarchy I would like to use as modules in an application for convenience purposes. To achieve this now I apply code that is alike to the snippet 1. The proposed config option would allow optimizing a little in such situation.

unscriptable commented 11 years ago

Heh, this may be all that's needed in the config routine:

var predefined, name = cfg['predefined']
for (name in predefined) _define(name, predefined[name]);
gamtiq commented 11 years ago

It seems the needed code should be a bit more complex. ;-)

var name, predefined;
if (predefined = cfg['predefined']) {
    for (name in predefined) {
        _define(name, predefined[name]);
    }
}