I really need to import js file on top of scripts.
I really need to import js file on top of scripts… is it possible ? if not, can it be add in next update ? Pierre Hébert
I’m making some API for thing that Lukas Morawietz use to do in it’s scripts (like read/write to file system)
and adding 30 lines on top of any script just to add a class to manipulate file system is not really good I think…
First comment is the FileRW API to manipulate file system
Second comment is the last script from Lukas Morawietz using my API… (just to make an idea..) (it’s more readable 😉 )
]]>« Next code snippet: change the homescreen (Previous Post)
< ![CDATA[
function FileRW(path) {
this.fileObject = new File(path);
}
FileRW.init = function() {
LL.bindClass(“java.io.FileReader”);
LL.bindClass(“java.io.BufferedReader”);
LL.bindClass(“java.io.FileWriter”);
LL.bindClass(“java.io.File”);
};
FileRW.prototype.read = function() {
var fileReader = new FileReader(this.fileObject);
var r = new BufferedReader(fileReader);
var s = “”;
//read the file
var l = r.readLine();
while(l != null) {
s += l;
l = r.readLine();
}
//return the file content
return s;
};
FileRW.prototype.write = function(str) {
var w=new FileWriter(file);
w.write(str);
w.flush();
w.close();
};
]]>
< ![CDATA[
import “FileRW.class.js”;
// import java classes
FileRW.init();
LL.bindClass(“java.lang.System”);
// set Desktop with Id as Homescreen
setHomeDesktop = function(newId) {
// this file contains desktop properties
var file = new FileRW(“data/data/net.pierrox.lightning_launcher_extreme/files/config”);
var s = file.read();
// change the property
var data = JSON.parse(s);
data.homeScreen = newId;
// write the changed stuff back to the file
file.write(JSON.stringify(data));
restartLauncher();
}
function restartLauncher () {
System.runFinalization();
System.exit(0);
}
]]>
< ![CDATA[
In order to re-use some code, you need to load it from once at startup, from a container load event (most probably some desktop – there is no “app startup” event yet). Loading mean either executing a script from existing ones, or loading it from a package or a fly using “eval”.
The loaded scripts need to store definitions in the root object of the scope, which available from all scripts (for historical reasons this is a Window object, that can be accessed by “this” or “self”).
I suggest to use some namespace when storing data in the root object.
For instance:
this.your_namespace.FileRW = function(path) { … }
When run from the top level scope of the script, “this” will point to the owner object, which is the window (but now you know that 😉 )
]]>
< ![CDATA[
Thanks for the tips! I’m going to test that 😉
]]>
< ![CDATA[
Question :
If I do : (bew is my namespace)
this.bew.FileRW = function() {
this.myVar = 5; // own by a FileRW instance
}
this.bew.FileRW.globalVar = 2;
this.bew.FileRW.prototype.read() {
// how to make the difference between
alert (this.myVar);
// and
alert (this.bew.FileRW.globalVar);
}
]]>
< ![CDATA[
correction: I understand, thanks !
the top this is only needed when defining functions & objects…
newt, just use :
bew.FileRW.globalVar
]]>