JS Require

    Nov 13 2014 | 12:12 am
    In the Max 7 release notes it mentions 'js require implementation' and also 'buffer~: exposed to JS', is there any information about these?

    • Nov 13 2014 | 12:38 am
      Max now supports two new functions for having JS files which use other JS files for functionality without requiring them to be in the jsextensions folder and universally loaded by all JS objects:
      1. var exports = require(filename) which works like CommonJS 1.0
      For example with the following in a file called require-example.js
      // for require, the module must be defined by assigning anything // the module wishes to export as properties of an "exports" property // This exports property is the return value from the require function. // modules included using require are always scoped to prevent collisions // in the top level script. Currently we don't support any of the other // typcial node.js module gobal properties like module.id, module.loaded, etc.
      exports.foo = 37; exports.bar = function () { post("I'm executing bar\n"); }
      This may then be included and used in another js file as the following:
      var reqmodule = require("require-example"); reqmodule.bar(); post("foo is "+reqmodule.foo+"\n");
      2. include(filename) which simply includes the javascript at the topmost level of the javascript file which calls this. This is more dangerous practice than the contained module definition of CommonJS and require(), but is useful in many situations
      example attached
    • Nov 13 2014 | 12:40 am
      (deleting superfluous info, Rob beat me to it!)
      Regarding jsbuffer, JS now has access to buffer~ data. See the attached example for a little peek (ahem) at what it can do. Also, check out the 'complex warping' tab in groove~'s help patcher.
    • Nov 13 2014 | 12:43 am
    • Nov 13 2014 | 12:48 am
      Wow, you guys are fast. These should get me going. Thanks!
    • Feb 15 2017 | 5:38 pm
      Hi I was hoping require would resolve using Max's file preferences. Instead the file has to be relative to the requiring scripts directory. Can you confirm this?
      ie I've put a js file in one of the folders listed in Max and I can't require it's file name.
    • Feb 15 2017 | 7:11 pm
      @ORANGE_GLASS I believe you have to give it an absolute path, or a path relative to the script that is requiring it.
    • Feb 23 2017 | 9:45 am
      OK thanks Ben for confirming.