有没有办法获取编辑器正在编辑的代理?
正常的工作流程是:
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时,这很有用。
谢谢。
有两种方法可以获取对给定编辑器正在处理的对象的引用。首先,一些简单的数据和一个简单的编辑器:
public class MyModel {
//sub properties...
}
public class MyModelEditor implements Editor<MyModel> {
// subproperty editors...
}
第一:与其实现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类SimpleEditor
的东西,尽管你可能还需要其他子编辑器:
现在,如果我得到同一代理的子编辑器
public class AntoherClass implements Editor<Proxy>{ someMethod(){ // method to get the editing proxy ? } }
这个子编辑器可以实现ValueAwareEditor<Proxy>
而不是Editor<Proxy>
,并保证在编辑开始时使用代理实例调用其setValue
方法。
在你的子编辑器类中,你可以实现另一个接口 TakesValue,你可以在 setValue 方法中获取编辑代理。
ValueAwareEditor也可以工作,但有所有你并不真正需要的额外方法。
找到的唯一解决方案。它涉及在调用驱动程序编辑之前调用上下文编辑。 然后,您有稍后要操作的代理。