PDA

View Full Version : btnInnerEl



Jehu
26 Apr 2011, 1:54 AM
Sometimes if i use setText(), the following error is thrown:



Uncaught TypeError: Cannot call method 'update' of undefined


The error comes from here: Code (http://dev.sencha.com/deploy/ext-4.0-beta3/docs/source/Button.html#line-840)
because 'me.btnInnerEl' is undefined.

I don't know why it is undefined. But take a look at setIcon(). There is a "if (btnInnerEl)" that is missing in setText()-Method.

Shouldn't be tested in setText for 'btnInnerEl' too?

mwoodman
22 Jun 2012, 12:58 PM
Ran into this today as well. In my use-case, I had a toolbar button whose setText() fails when the menu is open.

The problem was the scope of the function call to button.setText(). If I didn't ensure the button itself was "this", then invoking the setText() function would do bad things, starting with "
Uncaught TypeError: Cannot call method 'update' of undefined"

The following shows the simple fix of passing the button object as the scope argument when using setText:



Ext.define('App.foo.MenuButton', {
extend: 'Ext.Button',

initComponent : function(cfg) {

var me = this;

// Setup menu
me.menu = Ext.create('...')

// Update button text when menu title changes.
// Pass "me" as scope in 3rd arg. to ensure setText works
me.menu.on('titlechange', me.updateText, me);

me.callParent([arguments]);
},

/**
* Sets button text (using menu titlechange event args)
*/
updateText : function(menu, newtitle) {
this.setText(newtitle)
}
});

Animal
22 Jun 2012, 1:16 PM
When you set a breakpoint in updateText, what is "this"?

mwoodman
22 Jun 2012, 1:51 PM
When you set a breakpoint in updateText, what is "this"?

The master teaches with questions instead of answers. :)

I investigated further, and this came down to a problem of scope. The scope used when button.setText() is called was changing. Sometimes "this" was my menu, sometimes "this" was my button. The former cases caused setText() to break.

So the key is to make sure the correct scope is passed when button.setText() is called.

I've updated my earlier reply to reflect the lightbulb going on, however dimly.