2012-09-12 52 views
0

我想在GWT应用程序中使用编辑器,所以我已经阅读了官方文档。我也看过他的问题enter link description here及其答案,但我仍然无法弄清楚编辑的最终“目的”。作为一个示例情况下,假设与一些字段UiBinder的:在UiBinder中使用GWT编辑器

@UiField 
TextBox name; 

@UiField 
TextArea comment; 

@UiField 
ListBox type; 

... 

创建另外的方法editCustomer为:

private void editCustomer(CustomerProxy entity) { 

MyRequestFactory requestFactory = MyRequest.getRequestFactory(); 

CustomerRequestContext requestContext = requestFactory.customerRequest(); 

entity = requestContext.edit(entity); 

editorDriver.edit(entity, requestContext); 
} 

我认为与编辑器中的方法使得用于与数据库连接UiBinder的字段。这是如何完成的,基于通过“Save”Buttton在数据库中发送数据的常见方式?

@UiHandler("saveButton") 
void onSaveButtonClick(ClickEvent event){ 
???? 
} 
+0

是适当的添加评论解释什么是你发现斜或尴尬与问题,当你否决它。 – arjacsoh

回答

0

我一直在使用MVP模式一段时间,并有一些更复杂的编辑器。我发现把你的EditorDriver放到你的视图中是很好的,因为当你初始化它时,你可以将它绑定到你的特定视图。我的例子需要一个活动/视图接口/视图实现。

这是一项抽象活动,可以通过其他活动进行扩展,但我包含了相关内容。我已经剥离了很多代码,但是这应该让您了解使用编辑器的有用方法。我的编辑非常复杂,并且有很多子编辑。我只包括名称和描述。我们发现这是处理编辑的一个非常有用的设计模式。

public abstract class AbstractTaskBuilderActivity extends <E extends AnalyticsTaskProxy, R extends DaoRequest<E>> implements TaskBuilderView { 


/** 
* Create a new task. This will initialize any new lists. 
* @param context The RequestContext to use to create the task. 
* @param clazz The class type to be created. 
* @return 
*/ 
protected E create(R context, Class<E> clazz) { 
      // This is implemented in my inherited classes. 
    E editableAnalyticsTask = context.create(clazz); 
      // More initialization code expecially initializing arrays to empty so that 
      // any ListEditor sub editors will work. 


    return editableAnalyticsTask; 
} 

/** 
* Call to edit the task and update the dashboards. 
* @param context 
* @param task 
*/ 
protected void doEdit(R context, E task) { 
    RequestFactoryEditorDriver<? super AnalyticsTaskProxy, ?> driver = display.getEditorDriver(); 
    E editable = context.edit(task); 
    context.save(editable).with(driver.getPaths()).to(new Receiver<Long>() { 

     @Override 
     public void onFailure(ServerFailure error) { 
      display.showError(error.getMessage()); 
      super.onFailure(error); 
     } 

     public void onConstraintViolation(Set<ConstraintViolation<?>> violations) { 
      display.getEditorDriver().setConstraintViolations(violations); 
     } 

     @Override 
     public void onSuccess(Long response) { 
      clientFactory.getPlaceController().goTo(getSavePlace()); 
     } 

    }); 
    driver.edit(editable, context); 
} 


    /** 
    * Save the task. 
    */ 
     @Override 
    public void onSaveTask() { 

     RequestFactoryEditorDriver<? super AnalyticsTaskProxy, ?> driver = display.getEditorDriver(); 

     RequestContext request = driver.flush(); 
     request.fire(); 
    } 

} 

我的看法接口

public interface TaskBuilderView extends View { 

    public interface Presenter { 
     void onSaveTask(); 
    } 

    public RequestFactoryEditorDriver<AnalyticsTaskProxy, ?> getFactoryEditorDriver(); 

    public void setPresenter(Presenter presenter); 
} 

我的看法实施

public class AnalyticsTaskBuilderViewImpl extends ViewImpl implements AnalyticsTaskBuilderView, Editor<AnalyticsTaskProxy> { 

    interface AnalyticsTaskBuilderDriver extends RequestFactoryEditorDriver<AnalyticsTaskProxy, AnalyticsTaskBuilderViewImpl> { 
    } 

    /** 
    * UiBinder implementation. 
    * 
    * @author chinshaw 
    * 
    */ 
    @UiTemplate("AnalyticsTaskBuilderView.ui.xml") 
    interface Binder extends UiBinder<Widget, AnalyticsTaskBuilderViewImpl> { 
    } 

    /** 
    * Name component for the name of the analytics operation. 
    * This also implements {@link HasEditorErrors so it can show 
    * constraint violations when an error occurs. 
    */ 
    @UiField 
    ValueBoxEditorDecorator<String> name; 

    /** 
    * Description component that edits analytics operation description. 
    * This also implements {@link HasEditorErrors} so it can show 
    * constraint violations when an error occurs 
    */ 
    @UiField 
    ValueBoxEditorDecorator<String> description; 

    public AnalyticsTaskBuilderViewImpl(Resources resources) { 
     super(resources); 
     // Must initialize the view before calling driver initialize 
     initWidget(GWT.<Binder> create(Binder.class).createAndBindUi(this)); 
     driver.initialize(this); 
    } 

    @Override 
    public void setPresenter(Presenter presenter) { 
     this.presenter = presenter; 
     bindToPresenter(); 
    } 

    // Save the task 
    @UiHandler("saveTask") 
    void handleClick(ClickEvent clickEvent) { 
     presenter.onSaveTask(); 
    } 

    @Override 
    public RequestFactoryEditorDriver<AnalyticsTaskProxy, ?> getEditorDriver() { 
     return driver; 
    } 
}