[ 3 posts ]

BenNG

  • Joined: Wed May 23, 2012 8:56 am
  • Posts: 2
  • Offline
  • Profile
Tags:

Node.js + YUI3 + DOM manipulation on server side

Post Posted: Sun Jun 10, 2012 7:12 am
+0-
Hi Community !

At this moment, I'm using YUI 3.5.1 and node.js 0.6.19 and nothing else (I mean no Express, no BackBone, it's a choice :))

**** My problem is about, how to dynamically load a module on node.js environment.

Code:
var YUI = require("yui").YUI;
var fs = require("fs");
var document;
var window;
var jsdom = require("jsdom");

YUI().use(function (Y) {
 
  Y.namespace("Hello");
  Y.Hello.sayHello = function(){
    Y.log("Hello","debug");
  }

    fs.readFile(__dirname + "/views/index.html", "utf-8", function (err, data) {

        document = jsdom.jsdom(data);
        window = document.createWindow()
        YUI({
            doc: document,
            win: window
// add new modules to the current YUI instance
        }).use("node", "datatable", function (Y) {
     Y.Hello.sayHello();
   });

    });
});


YUI node's module seems to have the document and window defined before it's use. That why I try to load the node's module dynamically after the document and window initialization.
But it seems that instead of adding the node module to the current YUI instance, a brand new YUI instance is created.However I kind of copy/paste the example from the YUI user guides : YUI > Core > YUI Global Object

Code:
TypeError: Cannot call method 'sayHello' of undefined


Is YUI on node.js has a different environment than the browser ?
How can I solve my problem ?

Thank you

Juan Ignacio Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 638
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile

Re: Node.js + YUI3 + DOM manipulation on server side

Post Posted: Sun Jun 10, 2012 11:25 am
+0-
Actually, you'd run into the same problem in the browser if you were doing the same with an iframe for example. You're creating two different Y instances by calling YUI() twice, so the second Y, the one that tries to call sayHello, doesn't have the Hello namespace.

You could try creating a module for declaring your namespace. That way your code would look something like:
Code:
YUI.add('hello', function (Y) {
  Y.namespace("Hello");
  Y.Hello.sayHello = function(){
    Y.log("Hello","debug");
  };
});

fs.readFile(__dirname + "/views/index.html", "utf-8", function (err, data) {
  document = jsdom.jsdom(data);
  window = document.createWindow()
  YUI({
    doc: document,
    win: window
  }).use("hello", "node", "datatable", function (Y) {
     Y.Hello.sayHello();
  });
});

BenNG

  • Joined: Wed May 23, 2012 8:56 am
  • Posts: 2
  • Offline
  • Profile
Tags:

Re: Node.js + YUI3 + DOM manipulation on server side

Post Posted: Sun Jun 10, 2012 11:59 am
+0-
Thanks for answering !
I could be with modules yes, but I wanted to load dynamically the modules and then detached them after.
I mistook in the statement of my problem, I clearly created two sandboxes what I wanted was that : (like in the user guides :))

Code:
var YUI = require("yui").YUI;
var fs = require("fs");
var document;
var window;
var jsdom = require("jsdom");

YUI().use(function (Y) {

    Y.namespace("Hello");
    Y.Hello.sayHello = function () {
        Y.log("Hello", "debug");
    }

    fs.readFile(__dirname + "/views/index.html", "utf-8", function (err, data) {

        document = jsdom.jsdom(data);
        window = document.createWindow();
        Y.applyConfig({
            doc: document,
            win: window
        });
        Y.use("node", function (Y) {
            Y.Hello.sayHello();
        });
    });
});


and the result :)

Code:
debug: Hello
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum