You need to register the custom type before you load the document.
There are three different sections you want here:
- Before you load: register the custom type.
- On init: This happens exactly once per document. Here you want to be initializing the data model that every doc should have.
- On file loaded: This happens once per load. This is where you want to be setting up event listeners, etc.
Split your function into those three parts, and it should work.