Skip to content

Instantly share code, notes, and snippets.

@jthomas
Created June 21, 2012 15:56
Show Gist options
  • Save jthomas/2966649 to your computer and use it in GitHub Desktop.
Save jthomas/2966649 to your computer and use it in GitHub Desktop.
Rendering Dijit widgets server-side in NodeJs
var fs = require('fs'),
jsdom = require("jsdom").jsdom,
document = jsdom("<html><head></head><body></body></html>"),
window = document.createWindow();
// Fix window objects in global scope.
global.document = document;
global.navigator = window.navigator;
global.window = window;
// Read and evaluate dojo script in the current context
fs.readFile('./dojo.js', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
eval(data);
require(["dojo/has"], function (has) {
// Manually add event listener test as this was only included in
// the "host-browser" profile.
has.add("dom-addeventlistener", !!document.addEventListener);
// Resolve a dijit widget to read template string, this would
// be rendered within the page.
require(["dijit/form/Button"], function (Button) {
console.log("Raw template string:");
console.log(Button.prototype.templateString);
console.log("Instantiated template string:");
console.log((new Button()).domNode.innerHTML);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment