Labs/JS Modules: Difference between revisions

add the Observers module
(Typo fix)
(add the Observers module)
Line 57: Line 57:
* [http://hg.mozilla.org/labs/weave/index.cgi/file/8e3d60172f32/modules/log4moz.js log4moz.js] - Adorned
* [http://hg.mozilla.org/labs/weave/index.cgi/file/8e3d60172f32/modules/log4moz.js log4moz.js] - Adorned
* [http://hg.mozilla.org/labs/weave/index.cgi/raw-file/8e3d60172f32/modules/log4moz.js log4moz.js] - Raw (right-click to save)
* [http://hg.mozilla.org/labs/weave/index.cgi/raw-file/8e3d60172f32/modules/log4moz.js log4moz.js] - Raw (right-click to save)
== 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].
Ordinary JavaScript functions can be registered as observers:
function someFunction(subject, topic, data) { /* do something */ };
Observers.add(someFunction, "test");
Observers.remove(someFunction, "test");
And you can send notifications whose subject is an arbitrary JavaScript object:
let someObject = { /* some properties & methods */ };
Observers.notify(someObject, "test", null);
Note: you can use this module to observe notifications generated by any XPCOM component, and you can send notifications whose subject is an XPCOM component, but only JavaScript consumers of this API can send and receive notifications whose subject is an arbitrary JavaScript object.


== Preferences ==
== Preferences ==
canmove, Confirmed users
2,056

edits