组件回调的问题

梧桐168 2010-04-08 09:18:53
我写了一个com组件,用javascript来调用其中的一个方法,但调用完之后,我会不断的有返回状态回来,显示在网页上
想问下怎么回调给javascript,或者有什么其它的方法
...全文
155 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
firmbird 2010-04-09
  • 打赏
  • 举报
回复
有这么复杂吗?组件提供一个事件不就行了
梧桐168 2010-04-09
  • 打赏
  • 举报
回复

//*
function myObserver()
{
this.register();
}

myObserver.prototype =
{
observe: function(subject, topic, data)
{
alert('Performing 3+4');
},

register: function()
{
const cid = "@yoursite.com/MyXPCOM1;1";
var obj = Components.classes[cid].createInstance();
obj = obj.getService(Components.interfaces.nsIObserverService);
obj.addObserver(this, "myTopicID", false);
},

unregister: function()
{
var observerService = Components.classes['@yoursite.com/MyXPCOM1;1'].getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "myTopicID");
}
}
//*/
var my_xpcom = null;
var my_observer = new myObserver();
function MyComponentTestGo()
{
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const cid = "@yoursite.com/MyXPCOM1;1";
my_xpcom = Components.classes[cid].createInstance();
my_xpcom = my_xpcom.QueryInterface(Components.interfaces.IMyXPCOM1);
my_xpcom.observer = my_observer;

} catch (err) {
alert(err);
return;
}
var res = my_xpcom.Add(3, 4);
alert('Performing 3+4. Returned ' + res + '.');
}

梧桐168 2010-04-09
  • 打赏
  • 举报
回复
我写了一个小例子,但没成功,不知道哪里有问题,大致代码如下


NS_IMETHODIMP CMyXPCOM1::NotifyObservers(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
{
if (m_pObserver)
m_pObserver->Observe(aSubject, aTopic, someData);
return NS_OK;
}


/* long Add (in long a, in long b); */
NS_IMETHODIMP CMyXPCOM1::Add(PRInt32 a, PRInt32 b, PRInt32 *_retval)
{
*_retval = a + b;
NotifyObservers(this, "aaa", L"bbb");
return NS_OK;
}


sgzwiz 2010-04-08
  • 打赏
  • 举报
回复
function myObserver()
{
this.register();
}
myObserver.prototype = {
observe: function(subject, topic, data) {
// Do your stuff here.
},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "myTopicID", false);
},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "myTopicID");
}
}

Instantiation - this should be fired once you're ready to start observing (ex: a window's load event).

observer = new myObserver();

Destruction - this should be fired once you're done observing (ex: a window's unload event). Failure to do so may result in memory leaks.

observer.unregister();


nsresult
nsWebShellWindow::NotifyObservers( const nsString &aTopic, const nsString &someData ) {
nsresult rv = NS_OK;
// Get observer service.
nsIObserverService *svc = 0;
rv = nsServiceManager::GetService( "@mozilla.org/observer-service;1",
NS_GET_IID(nsIObserverService),
(nsISupports**)&svc );
if ( NS_SUCCEEDED( rv ) && svc ) {
// Notify observers as instructed; the subject is "this" web shell window.
nsCAutoString topic; topic.Assign(prefix);
topic.Append(";");
topic.AppendWithConversion(aTopic);
rv = svc->NotifyObservers( (nsIWebShellWindow*)this, topic.get(), someData.get() );
// Release the service.
nsServiceManager::ReleaseService( "@mozilla.org/observer-service;1", svc );
} else {
}
return rv;
}
sgzwiz 2010-04-08
  • 打赏
  • 举报
回复
可以采用nsIObserverService接口
https://developer.mozilla.org/en/nsIObserverService
SullenSun 2010-04-08
  • 打赏
  • 举报
回复
up。。
梧桐168 2010-04-08
  • 打赏
  • 举报
回复
我在网上找到了COM组件中调用JavaScript函数的方法
http://faceye.com/wiki/topic/402881b12625f10601262619531c0748
但我做的是xpcom在mozilla里调用,xpcom里类似的VARIANT是怎么用的
雪影 2010-04-08
  • 打赏
  • 举报
回复
js声明一个函数,COM组件内部事件触发时调用该函数.
ccpaishi 2010-04-08
  • 打赏
  • 举报
回复
JavaScript是可以接收回调函数的啊,你直接实现回调不行吗??
梧桐168 2010-04-08
  • 打赏
  • 举报
回复
非常感谢楼上各位的回复,我试试看,刚开始搞这 xpcom开发,很多细节还不熟悉

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧