Why can't I extend everyone's pocket in nowjs?
Asked Answered
S

1

9

I'm trying to provide functions in everyone's pocket of nowjs. I'd like to do so by _.extending everyone's pocket, i.e. everyone.now. For some reason which I cannot understand, _.extend fails to properly provide the function at the client side.

This is my current code:

var _ = require("underscore"),
    everyone = require("nowjs").initialize(app);

everyone.now.foo = function() {};

_.extend(everyone.now, {
    bar: function() {}
});

console.log(everyone.now.foo); // [Function]
console.log(everyone.now.bar); // undefined

On both the server and client sides, I can do now.foo() just fine. On the other hand, now.bar() fails because now.bar is not defined. This is the case on both the client and server sides. I tried to check for existence at the server side, as shown above on the last line. However, this line logs undefined.

Underscore's extend function (obviously) does work on other objects so I guess it has something to do with the "magical namespace" that nowjs uses.

How come extending doesn't work with everyone.now and how can I get it to work?


Edit 2: I digged some more into proxies. It seems like setting a property on a proxy by passing a variable as its name does not work. I removed my first edit because this testcase is more narrowed down.

Why is this not working? Is this a bug? (Most of the times I ask this myself I know it isn't, but this is really making me clueless...)

var proxy = Proxy.create({
    get: function(pr, name) {
        console.log("get called");
        return null;
    },

    set: function(pr, name, value) {
        console.log("set called");
    }
});

var key = "foo";

proxy["foo"] = "bar";
proxy[ key ] = "bar";

proxy["foo"];
proxy[ key ];

Log result:

set called
get called
get called

Apparently, proxy[ key ] = "bar"; does not cause set to be called on the proxy. Why is that?

Sinus answered 4/12, 2011 at 19:5 Comment(2)
Running that same thing on my system gets the expected result of two calls to each function. What version of node and node-proxy do you have?Wage
@Logan F. Smyth: I'm running on node 0.6.5 which came out yesterday. I'm running node with the --harmony_proxies flag, so I don't need the third party library. It might be that V8's implementation contains bugs at the moment whereas node-proxy does not. I'll check that out, thanks.Sinus
S
0

They posted a blog entry on the NowJS website on how to use node-proxy on Windows, instead of the native V8 implementation using the --harmony_proxies flag.

It appeared that the V8 version that Node currently uses contains several bugs with regard to proxies, which were causing the weird behaviour as outlined in the question. node-proxy, however, is a module that enables proxies (the core of the "magical namespace" of NowJS) without those bugs. (The bugs are fixed in a newer version of V8 as well, but that would require a custom build of Node.)

I just couldn't figure out how to build node-proxy on Windows (it's a .node addon; not a pure JavaScript one). In the above blog post they distributed the compiled module, and everything now works like a charm.

To fix:

  • Download the compiled module
  • Extract the folder to the node_modules folder and rename it to now
  • Don't run Node with the proxy flag

Edit: Node 0.7.0 uses V8 3.8.6 which also solves this issue. Just run with the --harmony flag and remove the reference to node-proxy.

Sinus answered 10/1, 2012 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.