How to get View instance after deserialization?


#1

I’m working on a package which has a custom subclass of ScrollView, let’s call it MyContentView.

I can serialize and deserialize the data correctly, but how on earth, in my top level controller code, do I find the actual MyContentView instance after deserialization? I can find the HTML using atom.workspaceView.find('.my-content-view') but this doesn’t seem to be connected to the view instance in any way. I can see that there is something like atom.workspace.getPanes()[x].items which contains my instance but am I supposed to just do a linear search to find it?

Thanks, Samuel.


#2

This is an interesting question.

Naively I would have said that if your top level controller needs to be aware of the deserialized view then you can probably make your views notify this controller that they were recreated using serialized data, something like this:

module.exports = 
class MyContentView 
  constructor: (serializedData) -> 
    if serializedData? 
      @notifyTopLevelController()
      @setupView(serializedData)

When it’s your top level controller that creates the views it can instantiate them without passing any arguments and use the setupView method to pass the initial data to the view. The view should return the data it receive in setupData as an object in the serialize method.

Now, by looking at the markdown-preview package there are these lines:

It seems that you can register your own deserializer at the global level, which mean you can call a method on your top level controller to create your views from serialized data. But as I never used this API I can’t tell much about it.


#3

Thanks man, I managed to get it all working: https://atom.io/packages/script-runner

The specific code is in lib/script-runner-view.coffee which is responsible for serialisation/deserialisation and finding the view instance is done in lib/script-runner.coffee where I manually search through atom.workspace.getPanes()[x].items to find it.

It seems to work fine. It feels like a hack thought. I might investigate using the URI functionality of workspace.