【问题标题】:GWT Editor frameworkGWT 编辑器框架
【发布时间】:2012-04-13 16:59:59
【问题描述】:

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

正常的工作流程是:

 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 时,这很有用。

谢谢。

【问题讨论】:

    标签: gwt editor


    【解决方案1】:

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

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

    首先:除了实现Editor,我们可以选择另一个同样扩展编辑器但允许子编辑器的接口(@98​​7654323@ 不允许子编辑器)。让我们试试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 类 SimpleEditor 的东西,尽管您可能还需要其他子编辑器:

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

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

    这个子编辑器可以实现ValueAwareEditor&lt;Proxy&gt;而不是Editor&lt;Proxy&gt;,并保证在编辑开始时它的setValue方法会被Proxy实例调用。

    【讨论】:

    • 是的,它可以工作,我只需要实现 ValueAwareEditor 并 setValue 自动设置代理。来自 API “其行为根据正在编辑的值发生变化的编辑器将实现此接口。”那是我的情况。 =)
    • 感谢 Colin 的帮助(尤其是 @Path("") :我很想做一个 @Path("this") 但这需要特殊处理......)现在我'想知道如何根据数据值从一个编辑器切换到另一个。我有一个应该更改表单的选择框(许多常见字段,但布局和一些字段出现/消失)。我对每种类型都有一个 UiBinder,我想在用户选择时从一个切换到另一个。我不喜欢根据情况创建它们并使其可见或不可见的想法。我想创建一个新的编辑器并为其添加属性
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      This is the only solution I found。它涉及在调用驱动程序编辑之前调用上下文编辑。然后你就可以稍后操作代理了。

      【讨论】:

        猜你喜欢
        • 2011-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多