有没有办法获得编辑器正在编辑的代理?
正常的工作流程是:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我有一个相同代理的子编辑
public class AntoherClass implements Editor<Proxy>{
someMethod(){
// method to get the editing proxy ?
}
}
Run Code Online (Sandbox Code Playgroud)
是的我知道我可以在创建后使用setProxy()将代理设置为子编辑器,但我想知道是否有类似HasRequestContext的内容,但是对于已编辑的代理.
当您在非UI对象中使用ListEditor时,这非常有用.
谢谢.
有两种方法可以获得对给定编辑器正在处理的对象的引用.首先,一些简单的数据和一个简单的编辑器:
public class MyModel {
//sub properties...
}
public class MyModelEditor implements Editor<MyModel> {
// subproperty editors...
}
Run Code Online (Sandbox Code Playgroud)
第一: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.
}
}
Run Code Online (Sandbox Code Playgroud)
这要求您实现上面示例中的各种方法,但您感兴趣的主要方法是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();
//...
}
Run Code Online (Sandbox Code Playgroud)
使用此方法,您可以调用self.getValue()以读出当前值.
编辑:看看AnotherEditor你已实现的,看起来你开始制作类似GWT类的东西SimpleEditor,尽管你可能也想要其他的子编辑器:
现在,如果我有一个相同代理的子编辑
Run Code Online (Sandbox Code Playgroud)public class AntoherClass implements Editor<Proxy>{ someMethod(){ // method to get the editing proxy ? } }
这个子编辑器可以实现ValueAwareEditor<Proxy>代替Editor<Proxy>,并保证setValue在编辑开始时使用Proxy实例调用其方法.