我想在我的 C++ 代码中添加一个事件处理程序。
我关注了 firebreath.org 中的文档(来自脚本的回调):
FB::JSObjectPtr doc = m_host->getDOMDocument()->getJSObject();
doc->Invoke("addEventListener", FB::variant_list_of("load", FB::make_callback(this, &mine::foo)));
但看到以下错误:
/home/dq/manager/mine.cpp: In member function ‘void mine::init()’:
/home/dq/manager/mine.cpp:284:119: error: no matching function for call to ‘variant_list_of(const char [5], FB::JSAPIPtr)’
/home/dq/manager/mine.cpp:284:119: note: candidates are:
/usr/include/firebreath/ScriptingCore/variant_list.h:122:5: note: FB::detail::VariantListInserter FB::variant_list_of(FB::variant)
/usr/include/firebreath/ScriptingCore/variant_list.h:122:5: note: candidate expects 1 argument, 2 provided
/usr/include/firebreath/ScriptingCore/variant_list.h:128:5: note: FB::VariantList FB::variant_list_of()
/usr/include/firebreath/ScriptingCore/variant_list.h:128:5: note: candidate expects 0 arguments, 2 provided
In file included from /home/deqing/manager/mine.h:51:0,
from /home/deqing/manager/mine.cpp:37:
/usr/include/firebreath/ScriptingCore/JSCallback.h: In function ‘FB::JSAPIPtr FB::make_callback(const T&, F, bool) [with T = mine*, F = void (mine::*)(), FB::JSAPIPtr = boost::shared_ptr<FB::JSAPI>]’:
/home/dq/manager/mine.cpp:284:118: instantiated from here
/usr/include/firebreath/ScriptingCore/JSCallback.h:47:107: error: request for member ‘get’ in ‘instance’, which is of non-class type ‘mine* const’
/usr/include/firebreath/ScriptingCore/JSCallback.h:49:97: error: request for member ‘get’ in ‘instance’, which is of non-class type ‘mine* const’
make[2]: *** [CMakeFiles/mine.dir/manager/mine.cpp.o] Error 1
make[1]: *** [CMakeFiles/mine.dir/all] Error 2
make: *** [all] Error 2
查看 make_callback() 的实现,我尝试了以下操作:
FB::JSObjectPtr doc = m_host->getDOMDocument()->getJSObject();
doc->Invoke("addEventListener", FB::variant_list_of("load")(FB::JSAPIPtr(new FB::JSCallback(FB::make_method(this, &mine::foo)))));
这次编译通过了,但是我的函数 - mine::foo() 没有被 document.load() 调用
通过在 chrome 中使用“检查元素”,在“事件侦听器”中,我可以看到为“加载”添加了一个新的侦听器。但是, listenerBody 是一个<JSAPI-Auto Javascript Object>
.
恐怕这就是为什么没有调用 mine::foo() 的原因,Javascript 不知道如何调用它,因为它不是函数,只是一个对象。
有谁知道如何完成这项工作?
我能想到的另一种方法是:
- 注册自定义事件处理程序
- 在初始化时触发自定义事件
我想使用类似的东西:
registerEventMethod("myevent", &mine::foo);
这样当 myevent 被触发时,可以调用 mine::foo() 。
这里的问题是,mine::foo 不是 JSObjectPtr,所以这段代码不起作用。
在这种情况下使用 registerEventMethod() 的正确方法是什么?