Search code examples
gwtmvpgwt-mvp

GWT/MVP: detecting change events in a table with proper MVP pattern


We're using gwt-presenter, but not really a question specific to that...

I've got a table with users in it. As I build the table in the view (from the data provided by the presenter), I need to add two action buttons ("Edit", and "Delete") at the end of the row.

What's the best way to assign click handlers to these buttons so the presenter knows which was clicked? Previous to this, we could pass a private field from the view to the presenter and attach a discrete click handler to that button. However, this method is rather rigid and doesn't work in this scenario very well.

Thanks in advance.


Solution

  • How about having the view allowing the subscription for edit/delete click events, registering internally the individual row click events, and then delegating the event handling to the ones registered by the view?

    I mean something like the following pesudo code:

    View:

    addRowEditClickHandler(ClickHandler handler) {
        this.rowEditClickHandler = handler; 
    }
    addRowDeleteClickHandler(ClickHandler handler) {
        this.rowDeleteClickHandler = handler; 
    }
    
    //... somewhere when setting up of the grid...
    
    rowEditButton.addClickHandler = new ClickHandler() {
        onClick(args) {
            this.rowEditClickHandler.onClick(args)
    
    }
    
    rowDeleteButton.addClickHandler = new ClickHandler() {
        onClick(args) {
            this.rowDeleteClickHandler.onClick(args)
    
    }
    

    Presenter:

    View view = new View();
    view.addRowEditClickHandler( new ClickHandler() {
        onClick(args) {
            doSomething();
        }
    });
    view.addRowDeleteClickHandler( new ClickHandler() {
        onClick(args) {
            doSomething();
        }
    });