nsIObserverService 编辑

xpcom/ds/nsIObserverService.idlScriptable This interface provides methods to add, remove, notify, and enumerate observers of various notifications. Inherits from: nsISupports Last changed in Gecko 0.9.6

The XPCOM nsObserverService implements this interface to provide global notifications for a variety of subsystems.

Implemented by @mozilla.org/observer-service;1 as a service:

var observerService = Components.classes["@mozilla.org/observer-service;1"]
                      .getService(Components.interfaces.nsIObserverService);

Method overview

void addObserver( in nsIObserver anObserver, in string aTopic, in boolean ownsWeak);
nsISimpleEnumerator enumerateObservers( in string aTopic );
void notifyObservers( in nsISupports aSubject, in string aTopic, in wstring someData );
void removeObserver( in nsIObserver anObserver, in string aTopic );

Methods

addObserver()

Registers a given listener for a notifications regarding the specified topic. See nsIObserver for a JavaScript example.

void addObserver(
  in nsIObserver anObserver,
  in string aTopic,
  in boolean ownsWeak
);
Parameters
anObserver
The nsIObserver object which will receive notifications.
aTopic
The notification topic or subject.
ownsWeak
If set to false, the nsIObserverService will hold a strong reference to anObserver. If set to true and anObserver supports the nsISupportsWeakReference interface, a weak reference will be held. Otherwise an error will be returned. (In most cases, you should use false.)

enumerateObservers()

Called to enumerate all observers registered for a particular topic.

nsISimpleEnumerator enumerateObservers(
  in string aTopic
);
Parameters
aTopic
The notification topic or subject.
Return value

Returns an enumeration of all registered listeners. See nsISimpleEnumerator.

notifyObservers()

This method is called to notify all observers for a particular topic. See Example.

void notifyObservers(
  in nsISupports aSubject,
  in string aTopic,
  in wstring someData
);
Parameters
aSubject
A notification specific interface pointer. This usually corresponds to the source of the notification, but could be defined differently depending on the notification topic and may even be null.
aTopic
The notification topic. This string-valued key uniquely identifies the notification. This parameter must not be null.
someData
A notification specific string value. The meaning of this parameter is dependent on the topic. It may be null.

removeObserver()

This method is called to unregister an observer for a particular topic.

void removeObserver(
  in nsIObserver anObserver,
  in string aTopic
);
Parameters
anObserver
The nsIObserver instance to remove.
aTopic
The notification topic or subject. This string-valued key uniquely identifies the notification. This parameter must not be null.

Example

This notifies all nsIObservers watching the "myTopicID" topic with an additional data parameter.

Components.classes["@mozilla.org/observer-service;1"]
          .getService(Components.interfaces.nsIObserverService)
          .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:69 次

字数:6832

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文