Susanna Huhtanen adventuring in software

Archive for January, 2012

Working with small JavaScript apps for GNOME3 has led me to a wild exploration around internet, the quest for knowledge. The quest led me to trying to figure out two things – how to use different files from the same folder in your JavaScript app, and how to make asynchronous HTTP requests. I can tell the quest was a tricky one but now I can present you (queue dramatic music) my findings:

The first problem was brought up by wanting to separate asychronous calls to a different file. How can I reference the other file in my gjs code? The solution ended up being very simple. Just replace the normal gjs yourapp.js with this:$ GJS_PATH=`pwd` gjs yourapp.js

in terminal in the folder your files are. Then the file can be included simply with:const SomeClass = imports.myotherfile;

Where SomeClass is the variable that will hold any functions exported by the other file (by having those functions defined into the global namespace there), and myotherfile.js would be the name of the file.

You can see what keys are available through imports by running this JavaScript code through gjs:

for (i in imports) {
print(i);
}

If you want to distribute your files in different folders the GJS_PATH environment variable must contain the correct path.

As we see, pretty easy but hard to come by amongst all the information of internet. Maybe one more post about it will ease the search for the next person trying to figure this out 🙂

Then we get onwards with our asynchronous calls. HTTP get, using async libsoup. Yet again a simple thing but hard to find.