daleharvey/pouchdb — pull request #1683 by yaronyg (2014-03-18 21:27:23+00:00)
Title: (#1602) - Make getHost pluggable in http.js

The idea is that an adapter can override getHost by doing something like the following:

```Javascript
function HttpPouchWrapper(opts, callback) {
...
var self = this;
self.getHost = HttpPouchWrapper.myAdaptersCustomGetHost;
return window.PouchDB.adapters.http.call(self, opts, callback);
}

HttpPouchWrapper.destroy = function (name, opts, callback) {
...
return window.PouchDB.adapters.http.destroy.call(this, name, opts, callback);
}

HttpPouchWrapper.myAdaptersCustomGetHost = function (name, opts) {
...
}
```

The reason for using the 'this' approach as opposed to just using an opts value is because of how PouchDB calls adapters. In constructor.js PouchDB will instantiate a new adapter using the line:
```Javascript
PouchDB.adapters[opts.adapter].call(self, opts, function (err, db) {
```
So the adapter wrapper has to maintain the 'self' (aka this) pointer anyway. So it's easy to just ride along.

In the case of destroy the logic is actually slightly different, at least when destroy is called on PouchDB's root object (e.g. PouchDB.destroy('foo')); In that case the call eventually goes to an adapter using the following line from setup.js:
```Javascript
PouchDB.adapters[backend.adapter].destroy(dbName, opts, function (err, resp) {
```
So in this case so long as the new getHost is defined directly on the adapter object then the call will work just fine.

And yes, this is confusing to me too. But I'm really not much of a Javascript maven. So if there is a simpler/cleaner way I'm all ears.