Visit Mozilla.org

nsIObserver

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

« Dokumentacja API XPCOM

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

Spis treści

[edytuj] Podsumowanie

nsIObserver is implemented by an object that wishes to observe notifications. These notifications are often, though not always, broadcast via the nsIObserverService.


nsIObserver jest zdefiniowany w xpcom/ds/nsIObserver.idl . Jest on skryptowalny i został zamrożony począwszy od Mozilla 0.9.6.

[edytuj] Kod interfejsu

[scriptable, uuid(DB242E01-E4D9-11d2-9DDE-000064657374)]
interface nsIObserver : nsISupports {
    void observe( in nsISupports aSubject,
                  in string      aTopic,
                  in wstring     aData );
};

[edytuj] Metody

[edytuj] observe

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

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

In general, aSubject reflects the object whose change or action is being observed, aTopic indicates the specific change or action, and aData is an optional parameter or other auxiliary data further describing the change or action.

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 behaviour 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.

While some observer-registration systems may make this safe in specific contexts, it is generally recommended that observe implementations not add or remove observers while they are being notified.

[edytuj] Powiązane interfejsy

nsIObserverService

[edytuj] Przykładowy kod

The following is an implementation of nsIObserver that can be registered with the preference service to be notified of changes in preferences (see Using preferences on MozillaZine for a complete example and for more information about the preference system in general).

var prefObserver = {
  // nsIObserver
  observe: function (aSubject, aTopic, aData) {
    if (aTopic == "nsPref:changed") { // observe preference changes
      // aData contains the name of the changed preference
      dump(aData+" changed!");
    }
  },

  QueryInterface: function(aIID) {
    if(!aIID.equals(CI.nsISupports) && !aIID.equals(CI.nsIObserver))
      throw CR.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

See also Using observers.