canmove, Confirmed users
2,056
edits
(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:// | 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]. | ||
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: | ||