Using observers: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
No edit summary
(add links to Xulplanet docs)
Line 2: Line 2:


==Observer class==
==Observer class==
Each observer may register for one or more '''topics'''. For each notification, its '''observe''' function is called with the '''subject''' (can be null), the '''topic''' and additional '''data'''. All three parameters can be freely used by the code invoking the notification, see "Notify all observers" below.
Each observer implements [http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html nsIObserver] interface. It may register for one or more '''topics'''. For each notification, its '''observe''' function is called with the '''subject''' (can be null), the '''topic''' and additional '''data'''. All three parameters can be freely used by the code invoking the notification, see "Notify all observers" below.


  function myObserver()
  function myObserver()
Line 34: Line 34:
           .getService(Components.interfaces.nsIObserverService)
           .getService(Components.interfaces.nsIObserverService)
           .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");
           .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");
==References==
*[http://xulplanet.com/tutorials/mozsdk/observerserv.html An article on observers at XulPlanet]
*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html nsIObserver] interface
*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserverService.html nsIObserverService] interface

Revision as of 00:53, 27 November 2004

An extension overlaying the Firefox browser window will have one "instance" for each window, that is completely separate from all other windows - you cannot share variables. There are several ways to implement cross-window communication - probably the most convenient one is using observers.

Observer class

Each observer implements nsIObserver interface. It may register for one or more topics. For each notification, its observe function is called with the subject (can be null), the topic and additional data. All three parameters can be freely used by the code invoking the notification, see "Notify all observers" below.

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");
  }
}

Instantiate & Register

Do the following in your init function (probably an onload handler) for each window:

var observer = new myObserver();
observer.register();

To do things cleanly, you should also unregister your observer when the window is closed.

Notify all observers

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

References