My solution looks as following (incomplete, only to show my approach):
$viewerElement.bind('documentLoaded', function (event) {
var changedEvent = new CustomEvent('webviewer-document-changed', {detail: {docId: self.webViewer.getInstance().docId}});
window.document.dispatchEvent(changedEvent);
[...]
}
Whenever the document changes, I dispatch an event to server.js.
self.documentRef = {};
function documentChanged(e) {
self.documentRef = firebase.database().ref(uniqueName + '/documents/').child(e.detail.docId);
}
window.document.addEventListener('webviewer-document-changed', documentChanged);
This code part listens to the event and changes the firebase reference to one specific document. Note that the member name changed from documentsRef to documentRef. documentRef therefore listens only to firebase events based on the ID of the document.
Please feel free to ask or suggest improvements.