2012-05-09 95 views
1

正如gwt文檔中所述,我嘗試在創建應用程序時遵循MVP設計模式。在使用簡單樹時,文檔中的example是直截了當的,是MVP和gwt的很好例子。在這個例子中,視圖被創建並且數據被賦予視圖來顯示。據我所知,這正是保持視圖,模型和主持人分開的方式。如何使用MVP模式創建CellTree

使用CellTree,數據填充發生在TreeViewModel的數據提供者內部。數據提供者不能在細胞樹之外拍攝,因此我需要在視圖中的細胞樹內部填充所有數據。現在,該視圖需要了解模型,並且MVP模式已被打破。我不想在將數據顯示給用戶之前將數據動態填充到單元樹,我需要編輯單元格樹中的數據並稍後將其保存爲不同的格式。

我的問題是如何在MVP設計模式中實現CellTree或一般Cell小部件?

回答

2

我已經使用了CellTable和MVP。

UI:

<g:HTMLPanel> 
    <g:ScrollPanel> 
     <p1:CellTable ui:field="cellTable" width="100%" styleName="style.cellTable" height="100%" /> 
    </g:ScrollPanel> 
</g:HTMLPanel> 

查看界面:

public interface SomeCellListView extends IsWidget { 
    void setPresenter(Presenter listener); 

     // Method to set the CellTable data 
    void setCellList(ArrayList<ObjectDTO> list); 

    public interface Presenter { 
     void goTo(Place place); 
     void doSomething(int id); 
    } 
} 

視圖實現:

public class SomeCellListViewImpl extends Composite implements SomeCellListView { 

... all Ui Binder stuff here 

@UiField(provided = true) 
CellTable<ObjectDTO>cellTable = new CellTable<ObjectDTO>(); 

SomeCellListViewImpl(){ 
    TextColumn<ObjectDTO> someColumn= new TextColumn<ObjectDTO>(){ 
     @Override 
     public String getValue(ObjectDTO o) { 
      return o.getSomeFieldValue(); 
     } 
    }; 
    cellTable.addColumn(someColumn, "column1"); 

    ... add other columns here 
} 

// This method is called from Presenter to set CellTable data 
public void setCellList(ArrayList<ObjectDTO> list) { 
    cellTable.setRowCount(list.size(), true); 
    cellTable.setRowData(0, list); 
} 

}

活性(或演示者):

//在構造函數中設置視圖和服務(獲取從ClientFactory視圖)

公共無效啓動(AcceptsOneWidget containerWidget,EventBus eventBus){

// Make RPC call 
this.service 
.getCellList(new AsyncCallback<ArrayList<ObjectDTO>>(){ 

    @Override 
    public void onFailure(Throwable caught) { 
     view.setError("Error fetching details"); 
    } 

    @Override 
    public void onSuccess(ArArrayList<ObjectDTO> result) { 
     view.setCelllist(result); 
    } 

}); 
view.setPresenter(this); 
containerWidget.setWidget(view.asWidget()); 

}

這裏,視圖已由ClientFactory創建。 View只包含CellTable的佈局。數據未在創建視圖時加載。當一個Activity開始時(又名Presenter),調用「start」方法。這裏我們讓RPC獲取Cell數據並調用視圖中的方法來設置數據。

我還沒用過CellTree。但是你總體上詢問了Cell小部件。因此想到分享這個。希望這可以幫助。

0

我與OP有同樣的問題。我閱讀了MVP教程第二部分,然後嘗試在我的應用程序中使用CellTable,同時仍然保留MVP架構師。但是我在這個部分混淆了:教程使用像Presenter這樣的語法,但是爲什麼只使用Presenter?