Thursday, June 28, 2012

Mootools Class in NodeJS global context - Second Approach

In my last article here, I described how to load Mootools class in NodeJS global context. What if I do not want to make use of include to load the class, is there a way I can still do it with require. So, lets rewrite the program, and check how to do it

Application.js
(function(){
 
var Application = this.Application = new Class(
{
    Implements: [process.EventEmitter],
    initialize: function()
    {
        console.log("App initialize");
    },
    compute: function()
    {
        console.log("App compute");
        this.emit("done");
    }
});
 
})();

So, now the main file contains the code as below

Server.js
require('./mootools');
require('./Application');
 
var app = new Application();
app.on("done", function() { console.log("App done"); });
app.compute();

Hope this will solve !

Wednesday, June 27, 2012

Mootools Class in NodeJS global context

In continuation of my previous articles, here I came across a requirement to load Mootools Class in NodeJS global context. Let me try to explain, NodeJS module system is one-on-one with module-on-file, which creates a problem. For example lets say I have a Class Application as described in my other article, defined in Application.js

Application.js
var Application = new Class(
{
    Implements: [process.EventEmitter],
    initialize: function()
    {
        console.log("App initialize");
    },
    compute: function()
    {
        console.log("App compute");
        this.emit("done");
    }
});
 
exports.Application = Application;

And now I try to make use in my Server as below

server.js
require('mootools');
var _Application = require('./Application');
 
var app = new _Application.Application();
app.on("done", function() { console.log("App done"); });
app.compute();

Not so good, you see _Application.Application() to create the object, this is fine if you want to have module to be in its own context, but what about if you don't ? Lets check how to load the Application class in global context so you could create the object in old fashion way var app = new Application();

To do so, you do not have to export the class as before, so the new code becomes as follows

Application.js
var Application = new Class(
{
    Implements: [process.EventEmitter],
    initialize: function()
    {
        console.log("App initialize");
    },
    compute: function()
    {
        console.log("App compute");
        this.emit("done");
    }
});

Create one file Include.js with the following contents
 
Include.js
var fs = require('fs');
var vm = require('vm');
 
(function(){
 
var include = this.include = function(path) {
    var code = fs.readFileSync(path);
    vm.runInThisContext(code, path);
}.bind(this);
 
})();

And the server file with following contents

server.js
require('./mootools');
require('./Include');
include('./Application.js');
 
var app = new Application();
app.on("done", function() { console.log("App done"); });
app.compute();

The Include.js file have a function include that does the job of loading the javascript file in global context. So you can access the class the good way.

Working with Mootools & NodeJS - Second Approach

In my last article here, I described how to make use of Mootools in a NodeJS applications. It included defining a class Application and then doing stuff with it. The class itself was defined in the same server.js file. This sometimes (very often for me) becomes a bottleneck as we start to work with one class per file approach. So, lets rewrite the program, create the file Application.js with following contents below
Application.js
var Application = new Class(
{
    Implements: [process.EventEmitter],
    initialize: function()
    {
        console.log("App initialize");
    },
    compute: function()
    {
        console.log("App compute");
        this.emit("done");
    }
});

exports.Application = Application;

So, now the main file contains the code as below
Server.js
require('./mootools');
var _Application = require('./Application');

var app = new _Application.Application();
app.on("done", function() { console.log("App done"); });
app.compute();

Working with Node.js & Mootools

This article will simply explain how to make use of Mootools with ServerSide JavaScript. I am focusing on NodeJS and will try to explain how to do it. I have worked on Mootools on client side in the past, but recently got a chance to work on NodeJS project, so it is based on my current experience with this project. Ok so lets get started with a simple example of creating a class, instantiating it object and calling some functions, here it goes.

server.js
require('mootools');
var Application = new Class(
{
    Implements: [process.EventEmitter],
    initialize: function()
    {
        console.log("App initialize");
    },
    compute: function()
    {
        console.log("App compute");
        this.emit("done");
    }
});
 
var app = new Application();
app.on("done", function() { console.log("App done"); });
app.compute();

You must have Server Side mootools in the current working directory, downlodable from Mootools Site
Try running the code.

E:\mootools-node>node server.js
App initialize
App compute
App done