<p>I have a ComboBox that I bind to a standard HTTPService, I would like to add an event listener so that I can run some code after the ComboBox is populated from the data provider.</p>

<p>How can I do this?</p>

[{'answer_id': 26553, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 0, 'selected': False, 'text': '<p>You can use <code>BindingUtils</code> to get notified when the <code>dataProvider</code> property of the combo box changes:</p>\n\n<pre><code>BindingUtils.bindSetter(comboBoxDataProviderChanged, comboBox, "dataProvider");\n</code></pre>\n\n<p><code>BindingUtils</code> lives in the <code>mx.binding.utils</code> package.</p>\n\n<p>I have a longer description of how to work with <code>BindingUtils</code> here: <a href="https://stackoverflow.com/questions/14247/flex-does-painless-programmatic-data-binding-exist#14261">Does painless programmatic data binding exist?</a></p>\n'}, {'answer_id': 26563, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 0, 'selected': False, 'text': '<p>You can also listen for the <code>ResultEvent.RESULT</code> on the HTTPService, that would be called slightly before the combo box got populated I guess, but it might be good enough.</p>\n'}, {'answer_id': 26580, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 1, 'selected': False, 'text': '<p>You can use a mx.binding.utils.ChangeWatcher as described <a href="http://www.adobe.com/livedocs/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&amp;file=databinding_091_08.html" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 26647, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 0, 'selected': False, 'text': "<p>Where are you adding the listener compared to the loading of the data?  Is it possible the data is being loaded, and the event fired, before you've added your listener?</p>\n"}, {'answer_id': 26695, 'author': 'mmattax', 'author_id': 1638, 'author_profile': 'https://Stackoverflow.com/users/1638', 'pm_score': 0, 'selected': False, 'text': '<p>@Herms</p>\n\n<p>The listener is definitely added before the web service call, here is an example of what my code look like (I simplified lots of things...):</p>\n\n<p>I have this flex component:</p>\n\n<pre>\n<code>\npublic class FooComboBox extends ComboBox\n{\n    private var service:HTTPService = null;\n    public function ProjectAutoComplete()\n    {\n        service = new HTTPService();\n        service.url = Application.application.poxmlUrl;\n        service.addEventListener(FaultEvent.FAULT,serviceFault);\n        service.addEventListener(ResultEvent.RESULT,resultReturned);\n\n\n        this.addEventListener(FlexEvent.DATA_CHANGE,dataChange);\n    }\n    public function init():void\n    {\n        var postdata:Object = {};\n        postdata["key"] = "ProjectName";\n        postdata["accountId"] = Application.application.accountId\n        service.send(postdata);\n    }\n    private function resultReturned(event:ResultEvent):void\n    {\n        this.dataProvider = service.lastResult.Array.Element;\n        // thought I could do it here...but no luck...\n    }\n    private function dataChange(e:FlexEvent):void\n    {\n        // combobox has been databound\n        mx.controls.Alert.show("databound!");\n    }\n    ...\n}\n</code>\n</pre>\n\n<p>and then in a mxml file I have the FooComboBox with id "foo" and I call:</p>\n\n<pre>\n<code>\nfoo.init();\n</code>\n</pre>\n\n<p>I need to execute some code after the combobox is <strong>completely</strong> databound...any ideas?</p>\n'}, {'answer_id': 26736, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 0, 'selected': False, 'text': "<p>Maybe the event doesn't trigger when the data provider is first set?  Try setting the data provider to an empty array in the constructor, so that it's definitely <em>changing</em> instead of just being initially assigned later in your resultReturned() method.  I've no clue if that will help, but it's worth a shot.</p>\n\n<p>Also, you're setting the provider to lastResult.Array.Element.  That looks a little suspicious to me, as the data provider should probably be an array.  Granted, I have no clue what your data looks like, so what you have could very well be correct, but it's something I noticed that might be related.  Maybe it should just be lastResult.Array?</p>\n"}, {'answer_id': 27467, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 0, 'selected': False, 'text': "<p>In your example code, try running <code>validateNow()</code> in the <code>resultReturned</code> method. That will force the combo box to commit its properties. The thing is that even though the property is set the new value isn't used until <code>commitProperties</code> is run, which it will do at the earliest on the next frame, <code>validateNow()</code> forces it to be done at once.</p>\n"}, {'answer_id': 3366706, 'author': 'Shashi Penumarthy', 'author_id': 367464, 'author_profile': 'https://Stackoverflow.com/users/367464', 'pm_score': 2, 'selected': False, 'text': '<p>Flex doesn\'t have a specific data-binding events in the way that say ASP .Net does. You have to watch for the dataProvider property like John says in the first answer, but not simply to the combobox or its dataProvider property. Let\'s say you have a setup like this:</p>\n\n<pre><code>&lt;!-- Assume you have extracted an XMLList out of the result \nand attached it to the collection --&gt;\n&lt;mx:HttpService id="svc" result="col.source = event.result.Project"/&gt;\n&lt;mx:XMLListCollection id="col"/&gt;\n\n&lt;mx:ComboBox id="cbProject" dataProvider="{col}"/&gt;\n</code></pre>\n\n<p>Now if you set a changewatcher like this:</p>\n\n<pre><code>// Strategy 1\nChangeWatcher.watch(cbProject, "dataProvider", handler) ;\n</code></pre>\n\n<p>your handler will <em>not</em> get triggered when the data comes back. Why? Because the <em>dataProvider</em> itself didn\'t change - its underlying collection did. To trigger that, you have to do this:</p>\n\n<pre><code>// Strategy 2\nChangeWatcher.watch(cbProject, ["dataProvider", "source"], handler) ;\n</code></pre>\n\n<p>Now, when your collection has updated, your handler will get triggered. If you want to make it work using Strategy 1, <em>don\'t</em> set your dataProvider in MXML. Rather, handle the <em>collectionChange</em> event of your XMLListCollection and in AS, over-write the dataProvider of the ComboBox.</p>\n\n<p>Are these exactly the same as a databound event? No, but I\'ve used them and never had an issue. If you want to be <em>absolutely</em> sure your data <em>has</em> bound, just put a changeWatcher on the selectedItem property of your combobox and do your processing there. Just be prepared to have that event trigger multiple times and handle that appropriately.</p>\n'}]
['https://Stackoverflow.com/questions/26512', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1638/']