17. January 2015

HAXE and NodeJS

Continuing the sudy of javascript target of HAXE we will focus on NodeJS this time. Before we move on, lets remind ourselves the things to expect from the HAXE toolchain by going through the previous two introductions, here is the first and the second post.
Thats about HAXE, now a little about NodeJS. In NodeJS the concept of modules is very common to make some re-usable code. In general, modules hide everything by default. In class based languages we can call them as private elements. To make something public , we have to use exports keyword in NodeJS.

For example, if we have a file names one.js as

exports.publicFunction = function() {
  console.log('Hello World');
}

Now, in NodeJS, we can write something as

var oneObj = require('./one');
oneObj.publicFunction();

Thats first kind of modules we have. There is another kind of modules in NodeJS, where instead of exposing each individual elements from the modules, it exposes a Constructor function. This is done with module.exports in stead of only exports.

module.exports.MyObjConstructor = function() {
  console.log('New Object');
}

This second one is looked at as classical concepts of programming, where each JS file is kind of a Class file.
The dynamic and fun nature of javascript allows us to use the first way of exposing individual elements to actually expose a constructor function too. Is not it ?! Why we think about that! Because HAXE generated javascript is actually doing that. Smart.

more

13. January 2015

Dojo Toolkit, require and events.

Well, lets say this, if you are working on Dojo Toolkit, you are working on a legacy application. Since Dojo Toolkit is releasing its newer versions, you need to change your application to newer codebase. Currently Dojo Toolkit is leveraging AMD for modules and Javascript files. Which is very new for this toolkit and veteran developers in this toolkit may find its very different.

Previously, when we needed some JS code, its simply done by making a ‘require’ call.

dojo.require("dojo.string");
//then use it
dojo.string.trim("  I Like Trim Strings ");

Now it looks as

require(["dojo/string", "dojo/domReady!"], function(string){
  string.trim("  I Like Trim Strings ");
});

Well that may seems fine now, but then what happens when we need something inside that require itself? Actually, its simpler than it seems, we can have require inside another require too.

require(["dojo/domReady!"], function(){
    //DOM is ready, load another module
    require(["dojo/string"], function(string){
      string.trim("  I Like Trim Strings ");
    });
});

Thats nice. Here is the official documentation. The introduction to modern dojo is here.
Another nice utility is topic. This is kind of central event dispatching and listening station. All one has to do is, dispatch events through topic and listen through it too. These are called publish and subscribe of events.

require(["dojo/topic"], function(topic){
    topic.subscribe("some/topic", function(arguments){
        console.log("received:", arguments);
    });
    // ...
    topic.publish("some/topic", "one", "two");
});

The other kind of events are DOM events and they are handled with the ‘on’ module.

Happy coding.

11. January 2015

HAXE JS 102

We are going good with HAXE, take a moment to go through the official HAXE documentation.
This is a follow-up post to the Haxe JS 101. Its all about setting up the expectations from the language and the tool chain. Lets make some more things clear about the tooling and how the language works.

  • HAXE will produce one single JS file
  • We can force it to create one JS file for one .hx file
  • The created JS file, is by no means is a module JS file as in CommonJS or AMD

The contents of .hxml or the build file generally looks as

-main ApplicationEntry
-js app.js

While forcing each .hx file for an individual .js file, the build file would contain something as

-js com/jungle/Tree.js 
-cp src 
com.jungle.Tree
--next
-js com/jungle/Animal.js 
-cp src 
com.jungle.Animal

For a sample project and the options, have a look at this repo and the sample .hxml file is here with all the possible options for .js output.

The important thing is to remember that finally every JS project is compiled to a single JS file and HAXE does it in one go. It does not provide module export or any fancy things that other JS frameworks do to support JS look like a classical programming language. HAXE works with its own tool chain and provides well made JS output. Do not fight it to mock JS libraries, HAXE is more powerful than that. Even for NodeJS output, HAXE produces the final JS output, just a single .js file. Work with HAXE packages and classes and then compile it to make the final output, no nothing in between.

Happy Haxing.