5

有没有办法获取编辑器正在编辑的代理?

正常的工作流程是:

 public class Class implments Editor<Proxy>{
  @Path("")
  @UiField AntoherClass subeditor;


  void someMethod(){
   Proxy proxy = request.create(Proxy.class);
   driver.save(proxy);
   driver.edit(proxy,request);
 }
}

现在,如果我有同一个代理的子编辑器

public class AntoherClass implements Editor<Proxy>{
   someMethod(){
   // method to get the editing proxy ?
    }
 } 

是的,我知道我可以在创建后使用 setProxy() 将代理设置为子编辑器,但我想知道是否有类似 HasRequestContext 的东西,但用于编辑的代理。

当您在非 UI 对象中使用例如 ListEditor 时,这很有用。

谢谢你。

4

3 回答 3

7

您可以通过两种方式获得对给定编辑器正在处理的对象的引用。首先,一些简单的数据和一个简单的编辑器:

public class MyModel {
  //sub properties...

}

public class MyModelEditor implements Editor<MyModel> {
  // subproperty editors...

}

首先:我们可以选择另一个接口,而不是实现Editor,它也扩展了 Editor,但允许子编辑器(LeafValueEditor不允许子编辑器)。让我们试试ValueAwareEditor

public class MyModelEditor2 implements ValueAwareEditor<MyModel> {
  // subproperty editors...

  // ValueAwareEditor methods:
  public void setValue(MyModel value) {
    // This will be called automatically with the current value when
    // driver.edit is called.
  }
  public void flush() {
    // If you were going to make any changes, do them here, this is called
    // when the driver flushes.
  }
  public void onPropertyChange(String... paths) {
    // Probably not needed in your case, but allows for some notification
    // when subproperties are changed - mostly used by RequestFactory so far.
  }
  public void setDelegate(EditorDelegate<MyModel> delegate) {
    // grants access to the delegate, so the property change events can 
    // be requested, among other things. Probably not needed either.
  }
}

这要求您实现上述示例中的各种方法,但您感兴趣的主要方法是setValue. 您不需要自己调用这些,它们将由驱动程序及其代表调用。如果您计划对对象进行更改,则该flush方法也很好用 - 在刷新之前进行这些更改将意味着您在预期的驱动程序生命周期之外修改对象 - 不是世界末日,但以后可能会让您感到惊讶。

第二:使用SimpleEditor子编辑器:

public class MyModelEditor2 implements ValueAwareEditor<MyModel> {
  // subproperty editors...

  // one extra sub-property:
  @Path("")//bound to the MyModel itself
  SimpleEditor self = SimpleEditor.of();

  //...
}

使用它,您可以调用self.getValue()以读出当前值是什么。

编辑:看看AnotherEditor你已经实现的,看起来你开始制作类似 GWT class 的东西SimpleEditor,尽管你可能还需要其他子编辑器:

现在,如果我有同一个代理的子编辑器

public class AntoherClass implements Editor<Proxy>{
  someMethod(){  
    // method to get the editing proxy ?
  }
}

该子编辑器可以实现ValueAwareEditor<Proxy>而不是Editor<Proxy>,并保证setValue在编辑开始时将使用 Proxy 实例调用其方法。

于 2012-04-13T18:49:52.530 回答
2

在您的子编辑器类中,您可以只实现另一个接口 TakesValue,您可以在 setValue 方法中获取编辑代理。

ValueAwareEditor 也可以工作,但拥有所有你并不真正需要的额外方法。

于 2012-05-12T14:29:27.913 回答
0

这是我找到的唯一解决方案。它涉及在调用驱动程序编辑之前调用上下文编辑。然后,您可以稍后操作代理。

于 2012-04-13T17:31:14.123 回答