nsIObserver 编辑

xpcom/ds/nsIObserver.idlScriptable This interface is implemented by an object that wishes to observe notifications. These notifications are often, though not always, broadcast via the nsIObserverService. Inherits from: nsISupports Last changed in Gecko 0.9.6

Method overview

void observe(in nsISupports aSubject, in string aTopic, in wstring aData);

Methods

observe()

This method will be called when there is a notification for the topic that the observer has been registered for.

If you expect multiple topics/subjects, the implementor is responsible for filtering.

You should not modify, add, remove, or enumerate notifications in the implementation of this method.

void observe(
  in nsISupports aSubject,
  in string aTopic,
  in wstring aData
);
Parameters
aSubject
In general reflects the object whose change or action is being observed.
aTopic
Indicates the specific change or action.
aData
An optional parameter or other auxiliary data further describing the change or action.

Remarks

The specific values and meanings of the parameters provided varies widely, though, according to where the observer was registered, and what topic is being observed.

A single nsIObserver implementation can observe multiple types of notification, and is responsible for dispatching its own behavior on the basis of the parameters for a given callback. In general, aTopic is the primary criterion for such dispatch; nsIObserver implementations should take care that they can handle being called with unknown values for aTopic.

The most common implementation of nsIObserverService is the XPCOM observer service. With this implementation, it's safe (and common practice) for an implementation of nsIObserver to remove itself as an observer during the Observe callback, or to add or remove other observers. Be careful, though, because other uses of nsIObserver may not support these operations correctly within Observe.

Example

The following code is an implementation of nsIObserver that is registered to receive notifications for the "myTopicID" topic. See Observer Notifications for a list of built-in topics possible.

Observing preferences works slightly differently. See Code snippets:Preferences - Using preference observers for an example.

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 (for example a window's load event).

observer = new myObserver();

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

observer.unregister();

"Get Everything" - note that "*" is an acceptable value (be careful with this, because it observes many events, and can degrade performance). 

observer.register("*");

See also

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

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

发布评论

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

词条统计

浏览:38 次

字数:5454

最后编辑:7年前

编辑次数:0 次

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