PDA

View Full Version : [CLOSED] ext:Button: Adding listener to a click event



supera
Apr 04, 2012, 6:59 PM
Hi!

I'm creating my ext:window as javascript: It is working.
But, I wish pass the buttons of window as parameter of the function;
I'm creating a function below to create a button. But my problem is: I want set the javascript that be executed when the button is clicked.

In vb.Net, i do this:


function createButton(text as string, fn as string)
dim btn = New Ext.Net.Button
btn.Id = "btnOk"
btn.Text = text
btn.Listeners.Click.Handler = fn
return btn
end function


How I do it in Javascript?




function createButton(text, fn) {
var btn;

btn = Ext.create("Ext.button.Button", {
id: "btn" + text,
text: text
});
btn.on("click", "function () {" + fn + "}");
return btn;
}

geoffrey.mcgill
Apr 05, 2012, 12:43 AM
Hi,

The best way to figure this out is to configure in code-behind, then view the Page in a browser, then View > Source to see the client-side configuration script generated.

You can then take that script and pull out the piece you required.

Hope this helps.

supera
Apr 05, 2012, 12:35 PM
Hi Geoffrey... thanks for your suggestion...

Daniil gave me this tip before, and I have used enough.

But, in this case, The function in listener click not static... I wish pass this function as parameter, because, depending the window where I am, I want a different action on the OK button click of window. My window is created in Javascript code too.

My problem is transform a string with javascript function passed as parameter in a function associated to click listener.

In codebehind, it's easy:



btn.Listeners.Click.Handler = fn


Where 'fn' is a string with javascript function.

But, I don't know how I do it in Javascript.

I'm trying this way (in Javascript), but, don't works:



btn.on("click", "function () {" + fn + "}");

Daniil
Apr 05, 2012, 12:54 PM
In JavaScript I would suggest to pass a function instance as a parameter.

Then it will look this way:

function createButton (text, fnParam) {
var btn;

btn = Ext.create("Ext.button.Button", {
id : "btn" + text,
text : text,
listeners : {
click : {
fn : fnParam
}
}
});
return btn;
}

supera
Apr 05, 2012, 12:59 PM
In your suggestion, the 'fnParam' is a javascript function or a string contains java script function code?

Daniil
Apr 05, 2012, 1:00 PM
A JavaScript function.

It's my suggestion - pass a JavaScript function as a parameter.

supera
Apr 05, 2012, 1:04 PM
Daniil!

Thanks a lot for your help!

I pass the javascript function as parameter and works very fine... I will try now use this in my real source code...

If you want, you can close this thread... If I have new difficults, I create a new thread.

Thanks a lot, again