Search code examples
javagwtmvp

MVP multiples views combine to form an overall view


The GWT documentation comes with a tutorial on how to utilize the MVP pattern here. In this example, there are two views, and each replace the other as per the user action.

In these rather simple views, it didn't hurt much to cram all widgets that view has in one single class (view) only. But for a complex view, one would like to create individual views for components (with a corresponding presenter for each such component view), then combine those views into the overall views (this combined view may or may not have a separate combined presenter, since all sub-views already have corresponding presenters). Somewhat similar to creating individual widgets in separate classes that extend Composite, calling initWidget on them, and using them like mainPanel.add(new subPanel()) in the main panel.

So is it possible to do such thing in MVP pattern in GWT?


Solution

  • No, if you do so the entire DOM load in a single shot,even though you put if else conditions inside .

    When building large applications with GWT,Using MVP and code splitting is a must – otherwise, the entire application (i.e. JavaScript bundle) is downloaded in one chunk on the initial load of the application, which is a good recipe for frustrated users!

    By using standard MVP you can

    Isolate of User Interface from Business tier
    
    Easily interchangeable Views (user interfaces)
    
    Ability to test all code more effectively 
    

    I suppose you are expecting like below

    public class MainPageView extends ViewImpl implements MainPagePresenter.MyView {
    
    
        @UiField
        public HTMLPanel mainMenuPanel;
    
        @UiField
        public HTMLPanel mainContentPanel;
    
        @UiField
        public HTMLPanel mainFooterPanel;
    
    .
    .
    .
    .
    .etc
    

    Yes instead of panels as shown above , you can also use classes which have some elements inside.

    Update:

    To mainMenuPanel you can add your class like mainMenuPanel.add(new MyheaderClass()). Where MyheaderClass extends of Panel or Widget .So that the all elements in the Class add to the mainMenuPanel

    Inside your MyheaderClass class you may add labels, buttons ...etc by using this.add(mybutton)..etc