-
-
Save brikis98/1794839 to your computer and use it in GitHub Desktop.
var getName = require('name').getName; | |
exports.hello = function() { | |
return "Hello " + getName(); | |
}; |
<script type="text/javascript" src="/js/inject.js"></script> | |
<script type="text/javascript"> | |
require.setModuleRoot("http://example.com/js/modules"); | |
require.run("program"); | |
</script> |
<script type="text/javascript" src="/js/modules/name.js"></script> | |
<script type="text/javascript" src="/js/modules/hello.js"></script> | |
<script type="text/javascript" src="/js/modules/program.js"></script> |
exports.getName = function() { | |
return prompt("What's your name?"); | |
}; |
var hello = require('hello').hello; | |
alert(hello()); |
index.html:3
, reqiure
typo
@danmilon: fixed, thanks!
Interesting indeed, but I cannot get it to work.
I downloaded all the files here, plus the inject.js lib from the link in
the getting started guide
https://github.com/linkedin/inject/downloads
I changed require.setModuleRoot("http://example.com/js/modules");
to require.setModuleRoot("/js/modules");
and set up the remaining html for the index page (head and body).
I organized them like this:
|-index.html
|-js
_|-inject.js
_|-modules
___|-program.js
___|-hello.js
___|-name.js
At start I get no message at all.
It would be nice to have an all working .zip
to download and run.
Thanks.
@Serpenthelm: open up the console in your browser and see if there are any JS errors. My guess is that your module root isn't being set correctly. For example, if you are testing this code by opening up http://localhost:3000/index.html
in your browser, you'll need to update the code to require.setModuleRoot("http://localhost:3000/js/modules");
.
Ok, now it works. I had to rename a folder.
However, I can't get this lib to work offline (without a server).
I'm using Firefox+Firebug, I get no error, just this line
Porthole: Using built-in browser support
Then nothing. No error, no alert.
@Serpenthelm: I believe Inject uses XHR to fetch JS files, so you need a valid domain. This means running locally, without a server, won't work, since a file:///
path is probably not going to be recognized as a valid domain. @jakobo could comment more, but I think you'll need to fire up some sort of server (e.g. apache) to test so you can use localhost
as a domain.
See Introducing Inject, an open source JavaScript dependency management library for the browser for more information.