GWT 中的自定义插件事件
我们编写了一个浏览器插件。它会触发“mycustomevent”等自定义事件。我需要将这些自定义事件合并(即为其分配侦听器)到 GWT 中。这是我在 GWT 中的插件类。
class PluginObject extends Composite implements HasMyCustomEventHandler{
private Element plugin;
private HTML html;
private HandlerManager handlerManager;
public PluginObject(){
handlerManager = new HandlerManager(this);
html = new HTML("<object id='plugin0' type='application/x-ourplugin' width='300' height='300'>"
+"<param name='onload' value='pluginLoaded' />"
+"</object>");
initWidget(html);
plugin = html.getElement().getFirstChildElement();
}
}
以下是我将其添加到视图中的方法(我正在使用 gwtp)。它正在添加并且在页面中正常运行,我可以使用 JSNI 从 gwt 调用它的 js 函数。
@Inject
public MyPageView(){
...
plugin = new PluginObject();
panel.add(plugin);
}
我需要能够为从插件触发的事件分配处理程序。我希望能够在我的演示者中执行以下操作,但到目前为止我还无法将其连接起来。
@Override
protected void onBind(){
super.onBind();
registerHandler(getView().getPluginObject().addMyCustomEventHandler(
new MyCustomEventHandler() {
@Override
public void onMyCustomEvent(MyCustomEvent event) {
// call some other presenter stuff here...
}
}));
}
上面提到了一些额外的类。我定义了一个 MyCustomEvent 类、一个扩展定义的 EventHandler 的 MyCustomEventHandler 接口以及一个扩展定义的 HasHandlers 的 HasMyCustomEventHandler 接口,但将它们全部连接起来后我什么也没得到。为了简洁起见,我省略了它们,我很乐意添加任何可能有帮助的内容。
我可以在裸js中添加事件监听器。
obj.addEventListener("mycustomevent", function(){ foo(); }, false);
并每次捕获事件(foo() 被调用),因此我确信事件正在被触发。
我尝试将以下内容添加到我的 PluginObject 类中,但这只是我从搜索到的内容中猜测的。但它没有起作用。
public HandlerRegistration addMyCustomEventHandler(MyCustomEventhandler handler){
return handlerManager.addHandler(MyCustomEvent.TYPE, handler);
}
这里正确的方法是什么?
We have coded a browser plugin. It fires custom events like 'mycustomevent'. I need to incorporate (i.e. assign listeners for) these custom events into GWT. Here is my plugin class in GWT.
class PluginObject extends Composite implements HasMyCustomEventHandler{
private Element plugin;
private HTML html;
private HandlerManager handlerManager;
public PluginObject(){
handlerManager = new HandlerManager(this);
html = new HTML("<object id='plugin0' type='application/x-ourplugin' width='300' height='300'>"
+"<param name='onload' value='pluginLoaded' />"
+"</object>");
initWidget(html);
plugin = html.getElement().getFirstChildElement();
}
}
Here is how I am adding it in my View (I am using gwtp). It is being added and is functioning correctly in the page and I can call its js functions from gwt with JSNI.
@Inject
public MyPageView(){
...
plugin = new PluginObject();
panel.add(plugin);
}
I need to be able to assign handlers for the events fired from the plugin. I'd like to be able to do the following in my presenter but thus far I have been un able to get it wired up.
@Override
protected void onBind(){
super.onBind();
registerHandler(getView().getPluginObject().addMyCustomEventHandler(
new MyCustomEventHandler() {
@Override
public void onMyCustomEvent(MyCustomEvent event) {
// call some other presenter stuff here...
}
}));
}
The above mentions a few additional classes. I have a MyCustomEvent class defined, a MyCustomEventHandler interface that extends EventHandler defined, and a HasMyCustomEventHandler interface that extends HasHandlers defined but after wiring them all up I get nothing. I've omitted them for brevity, I'd be glad to add anything that might help.
I can add event listeners in bare js.
obj.addEventListener("mycustomevent", function(){ foo(); }, false);
and catch the event (foo() gets called) every time so I'm confident the events are being fired.
I've tried adding the following to my PluginObject class, but this is just me guessing from things I've found searching. It has not worked.
public HandlerRegistration addMyCustomEventHandler(MyCustomEventhandler handler){
return handlerManager.addHandler(MyCustomEvent.TYPE, handler);
}
What is the proper approach here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我最终在 PluginObject 中所做的事情。我很想听听改进它的方法。我对 subscribeToBrowserEvents() 和对“this”的引用挂断了一段时间。最初,我调用 addEvent 方法并引用
Which 在运行时引用了错误的位置。有关此问题的更多信息,请参阅这段 JSNI 文档。享受。
Here is what I ended up doing in the PluginObject. I'd love to hear ways to improve it. I got hung up a while on subscribeToBrowserEvents() and the reference to 'this'. Initially I was calling the addEvent method and referring to
Which was referencing the wrong place at runtime. More about the issue here in this bit of the JSNI docs. Enjoy.