atom/atom — pull request #3587 by nathansobo (2014-09-19 22:52:38+00:00)
Title: Add view provider API to Workspace

/cc @atom/core Worth review since I'm adding a major API here.

This PR is another step in move away from SpacePen and towards HTML 5 custom elements. It adds two methods to `Workspace`:

## ::getView(object)

This method can be called with any object in the workspace in order to get a view. You can use it to get the view corresponding to the workspace itself, panes, or pane items. It *always* returns a DOM node rather than a SpacePen view. In the near future, it will return HTML 5 custom elements for all our views in core, but for now it just returns the root DOM node of SpacePen views with a `__spacePenView` expando property pointing back at the view. A branch is already in progress to convert core views to custom elements.

```coffee
editorView = atom.workspace.getView(atom.workspace.getActiveTextEditor())
paneView = atom.workspace.getView(atom.workspace.getActivePane())

# this one is weird but worth it for consistency. getting the workspace view
# should be rare once the panel APIs are in place.
workspaceView = atom.workspace.getView(atom.workspace)
```

## ::addViewProvider

This is the other half of `::getView`. If you add a model object as a pane or panel item, the workspace's view needs to know how to present it on screen. This is how you teach workspace to do that:

```coffee
atom.workspace.addViewProvider
modelConstructor: TextEditor # Future name of Editor
viewConstructor: TextEditorElement # Future custom element for TextEditor
```

Custom elements are required to have a `::setModel` method so the model can be assigned since element constructors don't take arguments.

You can also provide a `createView` factory method which is passed the model. This mechanism provides more flexibility but is less convenient if all you want to is build a custom element.

## Next Steps

Next up: converting our core views to custom elements, which includes workspace views and text editors. Every model object in the workspace should have its view constructed via the new provider API. After that, we'll be in position to switch to custom tag names and explore using the shadow DOM.