We had a situation where an slideEvent.change was triggered, even though there was no event listener. There was an event handler added, but right after we had set the runtime values of an HSlider component. There was no event listener before we had set the values.
What we needed was: set the values after the HSlider fired the creationComplete event in a function, after the HSlider accepted these new values, the HSlider should start to fire a change event when a user interacts with the component and call a certain method. We did not want a change event when we set the initial minimum and maximum values.
After a small bughunt we found that our code fired the change event anyway – which we did not want … This happens because we are setting the props and the event listener in the same frame. Internally the component calls the inherited invalidateProperties method after the event listener is added, the event listener is added immediately, before the values are set. Talk about confusing theories.
This did not work for our situation:
private function initDateRange () : void { // set min and max dates for hslider component with( dateRange ) { labels = [ dateFormat.format( new Date( dateRange.values[0] ) ),dateFormat.format( new Date( dateRange.values[1] ) ) ]; minimum = dateAdd( 'date',-30,new Date() ).getTime(); maximum = dateAdd( 'date',60,new Date() ).getTime(); values = [new Date(),dateAdd( 'date',10,new Date() )]; } // even though we are adding the listener after we have set the props // it is still triggered by the code dateRange.addEventListener( SliderEvent.CHANGE, filterCarts, false, 0, true ); }
Our fix that does the trick:
private function initDateRange () : void { // set min and max dates for hslider component with( dateRange ) { labels = [ dateFormat.format( new Date( dateRange.values[0] ) ),dateFormat.format( new Date( dateRange.values[1] ) ) ]; minimum = dateAdd( 'date',-30,new Date() ).getTime(); maximum = dateAdd( 'date',60,new Date() ).getTime(); values = [new Date(),dateAdd( 'date',10,new Date() )]; } // wait one frame before adding the slider event change handler callLater( addChangeHandler ); } private function addChangeHandler () : void { dateRange.addEventListener( SliderEvent.CHANGE, filterCarts, false, 0, true ); }
Another mystery solved!
Be the first to share your knowledge earthling