Amos
Amos

Reputation: 1433

How to access a function defined in a previous YUI.use() call

[I'm a YUI newbie]

I'm writing a Chrome extension that needs to change the contents of a web page created using the YUI3 framework.

I've identified that the extension, which injects javascript that runs in the page after it is loaded, must call a function that was previously defined in a YUI.add() call.

The original YUI code that runs is something like this:

YUI.add("uuu", function (c) {
    ...
    c.theObject = niceStuff;
}
...
YUI().use("uuu", function (c) {
    c.theObject.doSomething();
}

Is it possible that after this code runs, I can access a function of c.theObject?

(I understand this might go against YUI3's nice sandbox mechanism, but it's what I need to get the job done here).

Upvotes: 2

Views: 579

Answers (3)

Josh Hibschman
Josh Hibschman

Reputation: 3714

One way to do it is to capture the YUI() instance after you 'use' it. Like this:

YUI().add("uuu", function (c) {
    c.theObject = 'foo';
})

var yInstance = YUI().use("uuu", function (c) {
    c.theObject = 'booyaa';
})

yInstance.use('uuu',function(c){
console.log(c.theObject)
})
    // booyaa

Upvotes: 0

Nick Husher
Nick Husher

Reputation: 1884

You might have problems because any time a YUI() instance is created, it builds you a new sandbox. With a few exceptions, YUI modules are completely boxed by their sandbox context. For example:

YUI().use('node', function(Y1) {
    YUI().use('node', function(Y2) {
        assert(Y1.Node === Y2.Node) // fails!
    });
});

It's very possible that you may not be able to access the specific instance of theObject that you need, if it's never assigned to a variable outside the sandbox function scope. If any instance of theObject will do, you can just call into the YUI API and get your own version to play with.

Upvotes: 1

lawnsea
lawnsea

Reputation: 6581

This works for me: http://jsfiddle.net/sMAQx/1/

Upvotes: 1

Related Questions