Wednesday, June 22, 2011

Adding external javascript

Sometimes you want to add your own js file. Unfortunately the concept of including in javascript is not straight forward.

In EyeOS you can have a hack to get your js included, the only concern I have now is that if you execute the same application several times, it will pile up the js files, I lack the experience to tell you if it's going to be a problem.

But for now I can give you the hack, based on the previous post about my first application:


Due to EyeOS' design, if you want to access to any file from outside the context of the application, files must be placed in eyeOS/extern folder. A quick browse would give you with some files and folders that eyeOS or apps are using.

For my example, we will place the test.js file in eyeOS/extern/js/ with the following code

alert("I'm in");

Then you replace the code, with the hack, of the file remotetime.js

function remotetime_application(checknum,pid,args){
 var includes = ["eyeos/extern/js/test.js"];
 var head = document.getElementsByTagName('head')[0];
 var script = document.createElement('script');
 script.type= 'text/javascript';
 for (var i = 0; i < includes.length;i++ ){
  script.id = includes[i];
  script.src= includes[i];
  head.appendChild(script);
 }
 var app = new eyeos.application.Remotetime(checknum,pid);
 app.drawGUI();
}

qx.Class.define("eyeos.application.Remotetime", 
  {extend: eyeos.system.EyeApplication,
   construct: function(checknum,pid){
    arguments.callee.base.call(this,"Remotetime",checknum,pid);
   },
   members: {
    drawGUI: function(){
     var mainWindow = new eyeos.ui.Window(this, tr("Remote Time"), 'index.php?extern=/images/16x16/apps/accessories-calculator.png');
     mainWindow.setLayout(new qx.ui.layout.VBox(3));
     mainWindow.setAllowMaximize(false);
     mainWindow.open();
     // Display setup
     var display = new qx.ui.basic.Label("").set({
      allowGrowX: true,
      allowGrowY: true,
      textAlign : "right",
      font: "bold",
      decorator: "main"
     });
     mainWindow.add(display);
     // Button setup
     var b2 = new qx.ui.form.Button("Get the time");
     b2.addListener('click',function(){
      eyeos.callMessage(this.getChecknum(),'getTime',null,function(hora){
       display.setValue(hora);
      },this);
      },this);
     mainWindow.add(b2);
    }
   }
});

When starting the application it would show you a pop up saying "I'm in", after pressing ok it will finish load the application and get the remote time.

No comments:

Post a Comment