Cliford Shelton
Cliford Shelton

Reputation: 21

How to execute a YUI function from Javascript?

How can i call a YUI function that is wrapped inside a YUI().use from javascript?

example Below is a YUI function "runShowAnim" which executes animShow.run(); for an animation effect...

 var runShowAnim = function(e) {
     animShow.run();
 };

I want this effect to happen when i validate something in a javascript function. I tried to call it as below. But it doesn't seem to work.

function notifyUser(message) {
   document.getElementById("msgArea").innerHTML = message;
   runShowAnim();
}

Upvotes: 0

Views: 2286

Answers (5)

PottyBert
PottyBert

Reputation: 1982

If you want to call a function that has been defined inside the closure (the function passed as the last parameter to YUI.use) from outside it, you need to expose the function globally.

Either define a global variable outside the closure and assign your function to it, or assign your function to the window object

i.e.

var runShowAnim;

YUI().use(function(e){
  runShowAnim = function(){alert('called');}
});

runShowAnim();

Upvotes: 0

Cliford Shelton
Cliford Shelton

Reputation: 21

I achieved this by sandwiching the YUI function completely inside a function and calling that function..

var runShowAnim = function() { 
    YUI().use('anim', 'node', function(Y) {
        var animShow = new Y.Anim({
        node: '#msgArea',
        to: { height: 50,opacity:1 }
        });
        animShow.run();
    });
}; 

now i can call runShowAnim without any problem like in the below sample function..

function notifyUser(message) { 
   document.getElementById("msgArea").innerHTML = message; 
   runShowAnim(); 
} 

Upvotes: 2

jebberwocky
jebberwocky

Reputation: 1089

YUI.thefunction()?

I think you need to call it with namespace too

something similar to

var X = function(){};
X.Y = function(){};
X.Y.Z = function(){};
X.Y.Z.foo = function(e){alert(e);}

//foo("me");<-error

X.Y.Z.foo("me");

Upvotes: 0

Marko
Marko

Reputation: 72222

I think you're missing the parentheses.

function notifyUser(message) {
   document.getElementById("msgArea").innerHTML = message;
   runShowAnim(); // right here
}

Upvotes: 0

Quentin
Quentin

Reputation: 943214

If you want to call a function, you have to suffix the function name with () and include 0 or more comma separated arguments between them.

runShowAnim();

If the function doesn't have global scope (as yours will have if it is declared inside a function passed to use()) and not passed outside in some way then you can only do this from the same scope.

Upvotes: 0

Related Questions