Created
March 10, 2013 14:07
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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]); | |
| } | |
| } | |
| }); |
Author
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.
Author
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
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.jsit will be defined as(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?