Observers JS Module Improvements

I’ve updated the Observers JS module with a number of changes. In particular, it’s now possible to specify a this object when adding an observer that is a method, so instead of doing this:

let MyObserver = {
onFoo: function(subject) {...},
onBar: function(subject) {...},
onBaz: function(subject) {...},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
observe: function(subject, topic, data) {
switch(topic) {
case "foo":
this.onFoo(subject);
break;
case "bar":
this.onBar(subject);
break;
case "baz":
this.onBaz(subject);
break;
}
}
}

Observers.add("foo", MyObserver);
Observers.add("bar", MyObserver);
Observers.add("baz", MyObserver);

You can do this:

let MyObserver = {
onFoo: function(subject) {...},
onBar: function(subject) {...},
onBaz: function(subject) {...}
}

Observers.add("foo", MyObserver.onFoo, MyObserver);
Observers.add("bar", MyObserver.onBar, MyObserver);
Observers.add("baz", MyObserver.onBaz, MyObserver);

Other changes include a couple of bug fixes and some backwards-incompatible API improvements. See the documentation for all the details (including steps to update from the previous version of the module).

 

Myk Melez

Myk is a Principal Software Architect and in-house entrepreneur at Mozilla. A Mozillian since 1999, he's contributed to the Web App Developer Initiative, PluotSorbet, Open Web Apps, Firefox OS Simulator, Jetpack, Raindrop, Snowl, Personas, Firefox, Thunderbird, and Bugzilla. He's just a cook. He's all out of bubblegum.