Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

  • JS exception with Button returned from ButtonClickEvent

    SmartGWT Pro: 11.1p_2017-12-07
    GWT 2.7
    Chrome 63.0.3239.108 (Official Build) (64-bit)

    Found a problem when upgrading from SmartGWT 4.1 to 6.1 with a component that uses the ButtonClickHandler and ButtonClickEvent of Dialog

    Standalone test case reproducing the problem
    Code:
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.Button;
    import com.smartgwt.client.widgets.Dialog;
    import com.smartgwt.client.widgets.events.ButtonClickEvent;
    import com.smartgwt.client.widgets.events.ButtonClickHandler;
    
    public class Test implements EntryPoint {
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void onModuleLoad() {
            SC.showConsole();
    
            final Dialog dialog = new Dialog();
            dialog.setMessage( "Test" );
            dialog.setIcon("[SKIN]/ask.png");
            Button button = new Button( "Break" );
            dialog.setButtons( button );
            dialog.addButtonClickHandler( new ButtonClickHandler() {
    
                @Override
                public void onButtonClick( ButtonClickEvent event ) {
    
                    Button source = event.getButton();
                    SC.say( source.getTitle() ); // Throws a JS exception when compiled
                }
            });
            dialog.show();
        }
    }
    Click on the Dialog button results in the following JS exception if you attempt to get the title from the button returned from the event.

    Code:
    16:04:23.942:MUP0:WARN:Log:com.google.gwt.core.client.JavaScriptException: (TypeError) : source.getTitle is not a function
        at dispatch(test-0.js)
        at $doFire(test-0.js)
        at $fireEvent(test-0.js)
        at $fireEvent_0(test-0.js)
        at fireEvent_0(test-0.js)
        at <anonymous>(test-0.js)
        at apply_0(test-0.js)
        at entry0(test-0.js)
        at buttonClick(test-0.js)
        at click(http://localhost:58080/test/test/sc/modules/ISC_Containers.js?isc_version=11.1p_2017-12-07.js)
    I ended up changing the application code to use the individual ClickHandler on Button to achieve my desired result, instead of using the generic Dialog ButtonClickHandler, but from this test case, is there an issue with the Button returned from ButtonClickEvent if getTitle() isn't supported when the code is compiled?

  • #2
    The issue here is that the button in the Dialog is actually an IButton, so trying to get it as a Button (of which IButton is not a subclass) causes problems. We've improved ButtonClickevent.getButton() to properly throw a ClassCastException in this case, and also deprecated it in favor of a new API, ButtonClickEvent.getTargetCanvas(), which returns a StatefulCanvas. That new API, which is now the recommended approach, should be present in the nightly builds dated 2018-01-10 and beyond for SGWT 6.1 and newer releases.

    An alternative solution with your existing code would be to force creation of the Button before setting it into the dialog, which would force the dialog to treat it as a widget rather than a template to build an IButton. You could do this by calling Canvas.completeCreation().

    Comment


    • #3
      I raised it because the same code worked in SmartGWT 4.1 but broke after the upgrade to 6.1 so I assumed something was changed between the releases. This clarifies how the API should be used going forward. Thanks!

      Comment


      • #4
        In later releases, we've strengthened and made more uniform the rules for how templates (uncreated wrappers) are handled, to avoid subtle errors. So you may have been relying on undocumented behavior - or you could've just been lucky. Your sample code works for us in the current SGWT release without error when run with -optimize 0, probably due to the similarity in the underlying method names in the implementations of Button and IButton (even though neither is Java-castable to the other type).

        Comment

        Working...
        X