Ext.fireEvent(‘function’) Falsely Returns True

A gotcha in ExtJS when using the code below

Ext.fireEvent(‘setDataViewErrWarnLineArr’, warnlinArr);

it reveals a bug in returning “true” when the listener was not actively listening.

The code below shows fireEvent being triggered before the object, Tier2PayrollWdw is created. Not being created first the listen parameter is not even created to be listening for the fired event. This also created a debugging issue in returning true.

Per Sencha documentation

fireEvent ( eventName, args ) : Boolean
Shorthand for Ext.GlobalEvents#fireEvent. Fires the specified event with the passed parameters (minus the event name, plus the options object passed to addListener).

An event may be set to bubble up an Observable parent hierarchy (See Ext.Component#getBubbleTarget) by calling enableBubble.

Available since: 6.2.0 PARAMETERS
eventName : String

The name of the event to fire.

args : Object…

Variable number of parameters are passed to handlers.

RETURNS :Boolean
returns false if any of the handlers return false otherwise it returns true.

returning “true” unless any of the handlers return false. Interesting, this would seem to indicate a handler can return “false” though I’ve not tested this with an event/function that would be expected to return false, doesn’t seem to address the real concern. As a programmer I would expect if I fire and event/function and that event/function doesn’t exist then an error should be thrown, surely not a response of “true”.  I can understand the desire to reduce what can be returned since we are talking about JavaScript here, but wouldn’t anyone else agree if the event/handler doesn’t exist it should in actuality throw an error? True on something that doesn’t exist is a false true because surely true should mean something ran.

listen: {
   global: {
       setDataViewErrWarnLineArr: 'setErrWarnArray',
       renderCompanyID: 'renderCompanyID'
   }
},


store.load({
    callback: function (records, operation, success) {
        var responseJson = JSON.parse(operation.getResponse().responseText);
        var warnlinArr = responseJson.warnlines;
        Ext.fireEvent('setDataViewErrWarnLineArr', warnlinArr);
        console.log(Ext.fireEvent('setDataViewErrWarnLineArr', warnlinArr));
        var Tier2PayrollWdw = Ext.create('ezWeb.view.transfermanager.payrollTier2Wdw');
        Tier2PayrollWdw.setWidth(Ext.getBody().getViewSize().width - 300);
        Tier2PayrollWdw.setHeight(Ext.getBody().getViewSize().height - 300);
        Tier2PayrollWdw.setTitle('Data View - ' + obj.$widgetRecord.get('OriginalFileName') + ' Transfer Date: ' + Ext.util.Format.date(obj.$widgetRecord.get('TransferTime'), 'm/d/Y'));
        Tier2PayrollWdw.show();
     }
});

setErrWarnArray: function(warnLinArr) {
    console.log('warningLineArr set');
    this.warningLineArr = warnLinArr;
},

From the debugging standpoint watch out for this situation as you too could spend hours wondering why your response is “true” and yet you don’t have the expected results of values in the fired event/function not being set. That is the error the code above threw, inside the event/function I had a console.log(warnlinArr) which returned the error it was not defined leading me to chase the rabbit thinking something inside the function wasn’t coded right when in fact the actual error was in the code shown above by triggering the event before the event was available.

Author: aallord

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.