0

I have a method in a products.js file like so:

var handler = function(errors, window) {...}

and would like to execute it within a jsdom env callback:

jsdom.env({
    html : "http://dev.mysite.com:3000/products.html",
    scripts : [ "http://code.jquery.com/jquery.js", "page-scrapers/products.js" ],
    done : function(errors, window) {
        handler(errors, window)
        }  
});

When executed, it tells me 'handler is not defined'. Am I getting close?

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40

2 Answers2

0

If you want a variable to be accessible to another file, you have to export it. http://nodejs.org/api/modules.html

//products.js
exports.handler = function(window, error) {...}

// another.file.js
var products = require('products.js');
jsdom.env({
    html : "http://dev.mysite.com:3000/products.html",
    scripts : [ "http://code.jquery.com/jquery.js", "page-scrapers/products.js" ],
    // This can be simplified as follows 
    done : products.handler
});

This sounds like a bad idea though, why would a handler be made into a global? I think you should restructure your code

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • I want one javascript handler per page request, so that people can work on handlers independently. It's global only to the jsdom.env() method. There may be efficiency issues doing it this way, bar any caching. This is all executing server-side, though. – Jeff Lowery Nov 05 '12 at 21:44
  • @JeffLowery I haven't heard of node.js on the client yet ;) I still don't like the idea of exporting handlers and making them global. That just tends to lead to spaghetti code – Ruan Mendes Nov 05 '12 at 22:00
  • The modules link was helpful. I don't see how using that mechanism is 'global', as each export var is scoped by module name. I'll post an answer to my question and perhaps you can offer some constructive critique. – Jeff Lowery Nov 05 '12 at 22:08
  • @JeffLowery It's true that it's a contained (namespaced) global, but it's still accessible to any code in the application... I probably shouldn't have judged, it's hard to know without seeing the code – Ruan Mendes Nov 05 '12 at 22:37
  • Oh, I see... you edited your response to add the code. Wasn't there originally. – Jeff Lowery Nov 05 '12 at 22:49
  • @JeffLowery I pointed you to the right answer (even before I edited the code to be 100% accurate), you don't expect me to run every single line of code I paste here on SO do you? It was enough information for you to see what the problem was, wasn't it? – Ruan Mendes Nov 05 '12 at 22:50
0

Context of the problem is to scrape data from an existing web site. We want to associate a javascript scraper for each page, and access the scraped data via URLs served up via a node.js server.

As suggested by Juan, the key is using node.js modules. The bulk of the hander method is exported from product.js:

exports.handler = function(errors, window, resp) {...

and then imported in the node.js-based server instance:

//note: subdir paths must start with './' :
var products = require('./page-scrapers/products.js'); 

This creates a reference to the method by name 'products.handler', which can then be called in the request handler:

var router = new director.http.Router({
'/floop' : {
    get : funkyFunc
}
})

var funkyFunc = function() {
var resp = this.res

jsdom.env({
    html : "http://dev.mySite.com:3000/products.html",
    scripts : [ "http://code.jquery.com/jquery.js"],
    done : function(errors, window) {products.handler(errors, window, resp)}
});
}

And that works.

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40