tags:

views:

56

answers:

2

I'm using jQuery with jsdom in my node.js app.

Moreover, I want to use jQuery plugins(e.g. jQuery.diff), but I cannot find how to do this. Is there any way out?

A: 

I have a library that I'm working on which is a port of jquery to node - bit.ly/node-jquery

The goal is to do var $ = require('node-jquery') and use jquery libraries or functions as normal

Alex Bosworth
Thanks, but I can use jQuery with jsdom.jQuerify(). I want to know how to evaluate a jQuery plugin's .js file in the node.js app.
masahiroh
+2  A: 

Create a script tag in your document to load your script into it. Example:

createWindow = function(fn) {
    var window  = jsdom.jsdom().createWindow(),
        script = window.document.createElement('script');

    jsdom.jQueryify(window, function() {
        script.src = 'file://' + __dirname + '/some.library.js';
        script.onload = function() {
            if (this.readyState === 'complete') {
                fn(window);
            }
        }
    });
}

createWindow(function(window) {
    // Do your jQuery stuff:
    window.$('body').hide();
});

Source: http://blog.davidpadbury.com/2010/10/03/using-nodejs-to-render-js-charts-on-server/

balu
Just to clarify, that jQueryify method is effectively doing the same thing as the some.library.js load call. Take a look at http://github.com/tmpvar/jsdom/blob/master/lib/jsdom.js#L34.
David Padbury
Just to save you guys some time: There are at the moment some issues with jsdom and nodejs >= 0.2.3. In the context of a script, window evaluates to {} because of the way sandboxing changed.
balu
Thanks. It works. I should have carefully read the package's document.
masahiroh