Labs/JS Modules: Difference between revisions

update docs to the latest version of the module and point to the new canonical location of the module
(update location of Preferences module)
(update docs to the latest version of the module and point to the new canonical location of the module)
Line 60: Line 60:
== Observers ==
== Observers ==


The [http://melez.com/mozilla/modules/Observers.js Observers module] provides an API for observing and sending notifications using the [http://mxr.mozilla.org/mozilla/source/xpcom/ds/nsIObserverService.idl observer service].
The [http://hg.mozdev.org/jsmodules/file/tip/Observers.js Observers module] provides an API for observing and sending notifications using the [http://mxr.mozilla.org/mozilla/source/xpcom/ds/nsIObserverService.idl observer service].


Ordinary JavaScript functions can be registered as observers:
You can register ordinary functions as observers:


  function someFunction(subject, topic, data) { /* do something */ };
  function someFunction(subject, topic, data) { /* do something */ };
  Observers.add(someFunction, "test");
  Observers.add(someFunction, "test");
  Observers.remove(someFunction, "test");
  Observers.remove(someFunction, "test");
You can also register objects that implement the [http://mxr.mozilla.org/mozilla/source/xpcom/ds/nsIObserver.idl nsIObserver interface] as observers:
let someObject = {
  observe: function(subject, topic, data) { /* do something */ }
};
Observers.add(someObject, "test");
Observers.remove(someObject, "test");


And you can send notifications whose subject is an arbitrary JavaScript object:
And you can send notifications whose subject is an arbitrary JavaScript object:
canmove, Confirmed users
2,056

edits