[lively-kernel] Loading JS files into world

Marko Röder m.roeder at photon-software.de
Tue Jan 13 18:51:42 CET 2015


Hi Marco -

I don’t think modify the bootstrap.js is what is necessary here. This is only for real core functionality that you would want in any page (also mine, etc.).
Modules are the way to go and as I can see, you already figured out quite a bit. Very good!

To create your own module, open the world menu and go to Tools -> System Code Browser. In the SCB, click on the … next to the two arrows (<, >) and go to your directory. Right click on your directory listing, select “add new file” and choose a name for your module. This will give you a JS (module) file that looks similar to the snippet of your original email. And yes, you can load an external library with exactly this additional requiresLibs() call. Additionally, you can add all the JS methods you want to have in all your worlds inside the toRun(function() { … }) body.
If you still want to attach it to the (current) world, wrap it with lively.whenLoaded(function(world) { … }) and attach it to world.
But you can also create your own classes in that module, add those methods there and reference your class from the calling side.

To make your module a requirement in your worlds, there are two ways.
1) In every world you want the code to run, open the SCB (described above), find the module, right click the module in the listing and select “add to world requirements”. Don’t forget to save the page. The code will be loaded immediately and the next time you load this world.
2) Add it to your user config (so that every world you load, no matter its dependencies) will have it. Open the SCB, go to your user directory and edit the config.js. Either put it in as a requirement in requires('user.yourUsername.YourModule') or inside the toRun function do module('user.yourUsername.YourModule').load(true); (true = synchronous loading).
The drawback here is that only you will run your user config and if someone else tries to load your worlds, those dependencies will be missing. (So I encourage you to use (1) even though it means that you will have to add this dependency to all of your worlds manually.)

You can find some additional information on modules, classes, etc. in:

http://lively-web.org/users/mroeder/lively-concepts.html <http://lively-web.org/users/mroeder/lively-concepts.html>
http://lively-web.org/users/robertkrahn/lively-cheat-sheet.html <http://lively-web.org/users/robertkrahn/lively-cheat-sheet.html>

And, of course, let us know if you need any more help and keep us posted on what you are trying to build with Lively :-)!

Best,

	- Marko ;-)


PS: I guess you have also seen http://lively-web.org/users/robertkrahn/Lively-101.html <http://lively-web.org/users/robertkrahn/Lively-101.html>. It does not really cover "low-level stuff” can be of good help!


> On Jan 13, 2015, at 2:50 AM, Marco Monteiro <marco at neniu.org> wrote:
> 
> Hello!
> 
> I've recently started playing with Lively and I have a couple of questions.
> 
> I have some helper JS methods that I want to use in different worlds. Currently I install the methods in the world morph and use $world.<method> in my code. I would like to extract these methods into a JS library file and import it into different worlds. By reading other posts in the mailing list, I found that, for example, http://lively-web.org/users/larswassermann/relax.html <http://lively-web.org/users/larswassermann/relax.html>, loads the http://lively-web.org/users/larswassermann/relax.js <http://lively-web.org/users/larswassermann/relax.js> file. I want to do the same thing in my worlds. How can I do that?  
> 
> I've been exploring and found the Preferences window. In that, I see a bootstrap group, which seems could be something that would allow me to add files JS files to the bootstrap, but when I try to edit the bootstrapFiles property, I get '["core/lively/Migration.js","core/liv...' in the editor, instead of the entire array.
> 
> Also, how can I load a generic JS library (for example https://sdk.amazonaws.com/js/aws-sdk-2.1.5.min.js <https://sdk.amazonaws.com/js/aws-sdk-2.1.5.min.js>) into the world and have AWS (defined as a global variable in the library) be a global variable in my world?
> 
> I think I read in the mailing list an answer to this:
> 
> module(<my-module>).requires().requiresLib({
>     url: "https://sdk.amazonaws.com/js/aws-sdk-2.1.5.min.js <https://sdk.amazonaws.com/js/aws-sdk-2.1.5.min.js>", loadTest: function() { return !!window.AWS; }
> }).toRun(function() {
> 
> });
> 
> if I can load my own library (earlier question).
> 
> Is there any documentation or other resources for this kind of (lower level) stuff? For example, how the bootstrap process works, etc.
> 
> Thanks.
> _______________________________________________
> lively-kernel mailing list
> lively-kernel at hpi.uni-potsdam.de
> http://lists.hpi.uni-potsdam.de/listinfo/lively-kernel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.hpi.uni-potsdam.de/archive/lively-kernel/attachments/20150113/f2ae57a7/attachment.html>


More information about the lively-kernel mailing list