Question

I want to get data from within YUI sandbox and pass it to another function. For example, if I have something like:

YUI().use('node','stylesheet', function (Y)
{
    var sheet = Y.StyleSheet('#myStyle');
    // Some logic...
    var styleSheetText =  sheet.getCssText("h1");
});

console.log(styleSheetText); //still undefined

How to get o value styleSheetText right in console.log? I don't now if it is JavaScript problem or YUI problem.

Was it helpful?

Solution

It is both. In JavaScript you cannot see the value of a variable outside of the function it is declared in. If you declare styleSheetText within the callback function for YUI().use(), you cannot see its value outside of that callback function.

It is also a YUI issue. The callback function to YUI().use() is going to execute asynchronously, that is, the time of execution is unpredictable but it will always be after that console.log() statement gets executed. Thus, even if you made styleSheetText global to get around the variable visibility issue, it would still show undefined.

Usually, you put all your code within the callback function for YUI().use(). That would fix both problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top