Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created March 10, 2013 14:07
Show Gist options
  • Select an option

  • Save pvdz/5128700 to your computer and use it in GitHub Desktop.

Select an option

Save pvdz/5128700 to your computer and use it in GitHub Desktop.
requirejs fooky wat Why won't it just work as the example says?
// using requirejs 2.1.5
// see second example after http://requirejs.org/docs/plugins.html#apiload
define({
load: function (name, req, onload, config) {
if (name.slice(0,7) === 'js/user') {
var parse = function(err, source){
if (err) return console.error(err), onload.error(err); // error ajax?
// process source
var par = toTree(source);
translate(par.tok.btree);
source = par.tok.tokens.map(function(t){ return t.value; }).join('');
// now load it. somehow. properly
onload.fromText(source);
//require(['foo'], onload); // would work, but not with `name`... ? also, examples say that's for older r.js version
};
// use local storage is available, otherwise ajax it
var source = localStorage.getItem(name);
if (source) parse(null, source);
else get(name+'.js', parse);
} else {
// let requirejs do it's normal thing
req([name]);
}
}
});
@pvdz
Copy link
Copy Markdown
Author

pvdz commented Mar 10, 2013

fromText is bound to 'foo' if orig dep is 'plug!foo'. So the text should not have a named module?

That's a little confusing to me.

Those user files define their own name. Additionally they are stored in a file, with the same name. So if there's a script in js/src/user/main.js it will be defined as

define('js/src/user/main.js', [], function(){ ... });

(and in my environment, it'll actually be defined as just main.js, I rewrite the paths in before passing them off to requirejs).

Is the problem I'm facing related to defined name vs file name?

@jrburke
Copy link
Copy Markdown

jrburke commented Mar 10, 2013

Ah yes, that would be the problem, the named ID. Also, using IDs that end in '.js' are treated like URLs and are not given the normal module ID treatments and transforms so they are discouraged.

Actually, it may work if the module ID is the same value as the resourceId part of pluginId!resourceId that was used to load it (as long as it is not a .js URL), but it is really best to keep the anonymous if you can.

@pvdz
Copy link
Copy Markdown
Author

pvdz commented Mar 10, 2013

Ah, I didn't change the name in define(<name>, ...) before passing it back to requirejs. Seems that was indeed the issue. Thanks for your time! :)

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