Quantcast
Channel: Adobe Community : Popular Discussions - InDesign Scripting
Viewing all articles
Browse latest Browse all 15932

AMD like module loading

$
0
0

Hi, I need some critics for the following code snippet. I am trying to implement an AMD like functionality with ESTK and was wondering if I was doing it the right way or the wrong way. ESTK already has the ability to load scripts via `#include` preprocessor but it loads the script at the current scope, which might lead to bigger problems like overriding local variables. My approach is to wrap this preprocessor with the `require()` function and do the script loading there. This way variable collision is avoided, considering `var` is explicitly used for all variable declarations.

 

function require(module) {    var __module__;    function define(callback) {        __module__ = callback();    }      // eval is considered evil, I don't know if this could be an exception    eval('#include "' + module + '";');    // This also has the same result as above but I'm not sure which one is better or faster.    // $.evalFile(File(module));    if (typeof __module__ === 'undefined') {        throw('Module not loaded');    }    return __module__;
}


(function () {
    // relative loading doesn't work. Yet.    var moduleA = require('/absolute/path/to/modulea.jsx');    var moduleB = require('/absolute/path/to/moduleb.jsx');    $.writeln(moduleA.prop1);    $.writeln(moduleA.prop2);    $.writeln(moduleB.prop1);    $.writeln(moduleB.prop2);
}());

 

I don't know how to attach files here, please just save the code below as modulea.jsx and moduleb.jsx and change the path above where these files are required/loaded.

 

 

Save this as modulea.jsx

define(function () {    return {        prop1: 'module A prop 1',        prop2: 'module A prop 2'    }
});

 

Save this as moduleb.jsx

define(function () {    return {        prop1: 'module B prop 1',        prop2: 'module B prop 2'    }
});

Viewing all articles
Browse latest Browse all 15932

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>