nsIObserver 编辑
xpcom/ds/nsIObserver.idl
Scriptable 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.6Method 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论