Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJs didn't fire select event on ComboBox

I have a problem with ExtJs combobox, consider I have a combobox that has 4 items and a callback function in select event on combobox.

When I'm going to set the combobox selected value with setValue(), ExtJs don't fire select event.

How can I fix this problem?

Should I fire this event after setValue() by myself?

like image 850
Afshin Mehrabani Avatar asked Jan 10 '13 07:01

Afshin Mehrabani


2 Answers

I can tell you what the sencha support told for that one year ago:

Well, event is, by definition, a function call triggered by a user action and there's no user action when you call select.

Anyway, the "fix" is easy: you know that you call select so just after calling select you can call your select listener function.

It depends on your case what you should do. For me I hanged my implementation so that I was able to call re responsible method but on the other hand I don't see any downside when you fire the event yourself. So I think it is up to you which approach you like more.

Here is a example how you can fire the event by yourself (partly from the comment by @JohanHaest)

To make it simple I say you have only single selection enabled

var record = combo.store.getById(id);
combo.select(id);
combo.fireEvent('select', combo, record);

or

combo.select(model);
combo.fireEvent('select', combo, model);

There is a possible Hack in the current release (4.1.3) when setting a second argument on the select method to true. This will, according to the sourcecode, fire the select event. But the First argument has to be a Model instance.

// source-snipped
select: function(r, /* private */ assert)

So calling

combo.select(model, true);

will fire the select event but this behavior may change at any time (version) cause the assert is marked as private

like image 158
sra Avatar answered Oct 15 '22 11:10

sra


I came across this because I was incorrectly using valueField on combobox... I'm not sure what this is supposed to do but if you have it set incorrectly it breaks the combobox - it prevents the change event from firing at all and stops the select event from firing more than once.

My guess is its so you can have multiple objects in the list that represent the same item and the valueField represents the key so if you set it wrongly then the key is undefined for everything and therefore the same for everything. Which means nothing after the first set counts as a new value.

It seems like pretty niche behaviour (and not what I wanted) so I just stripped it out in my case and things started working.


Also I think the event you want is the change event - the documentation (for 4.1) says

Fires when the value of a field is changed via the setValue method.

whereas select only fires when someone clicks the combobox

Just a bit of an aside but I'd also question using an event for this - you know you've called setValue so why don't you just do the thing that you want to happen after you call set value. Events have a tendency to make code more confusing and if you dont need multicasting then its normally better to not bother with them.

like image 30
JonnyRaa Avatar answered Oct 15 '22 13:10

JonnyRaa