PDA

View Full Version : how to call same a function from several button tab event?



linuxyf
22 Nov 2011, 5:47 PM
there are several buttons in a view, i want to do same thing when i tab these buttons, so i want to invoke a function to finish this task.
xtype:'button',
name: 'delbtn',
text:'DEL',
handler: function() {
limit = 'ok' + this.getText();
Test(limit);
}

limit: ''
Test: function(mi){
Ext.Msg.alert('me', mi);
}

i get the exception: Uncaught ReferenceError: Test is not defined

how to call same function from several button click event?

mitchellsimoens
23 Nov 2011, 9:45 AM
MVC would be perfect for this!

mishoboss
13 Dec 2011, 7:50 AM
Why should be used MVC to just to do that?

I have a button and I have attached a handler to it: handler: buttonPressed

var buttonPressed = function(){
}

Now the question is how to send some parameters to this function? Or how the function knows which button initiated it?

rdougan
13 Dec 2011, 9:47 AM
MVC is perfect because you can use ComponentQuery to add a listener to many components, using a simple selector.

As for your problem; your first post just will not work. You are defining your function inside the configuration block, which just isn't how javascript works. You need to define it outside that block, or extend the class and define it in there.


var foo = function(a, b) {
console.log(a, b);
};

Ext.Viewport.add({
xtype: 'container',
items: [
{
xtype: 'button',
handler: foo
}
]
});


Ext.define('MyButton', {
extend: 'Ext.Button',

config: {
text: 'Tap me!'
},

initialize: function() {
this.callParent();

this.on({
scope: this,
tap: function() {
this.foo('a', 'b');
}
});
},

foo: function(a, b) {
console.log(a, b);
}
});

Ext.Viewport.add(Ext.create('MyButton'));

mishoboss
13 Dec 2011, 10:00 AM
I just can't get it...
For example I tried your first code. I made a handler that points to a function created outside the block. The function fires (the function fired also inside the block), but how can I pass some parameters to it? Where I send these 'a' and 'b'?

rdougan
13 Dec 2011, 10:20 AM
Here is a very simple example:


Ext.setup({
onReady: function() {
var foo = function(a) {
console.log(a);
};

Ext.Viewport.add({
xtype:'button',
text: 'Hello',
handler: function() {
foo(this.getText());
}
});
}
});