Allow params to be passed to ViewRegistry::createView

Now we call initialize on the created view instead of setModel, which
emphasizes the fact that it’s only called once and allows arbitrary
properties to be passed through during creation.
Esse commit está contido em:
Nathan Sobo
2014-11-30 15:42:14 -07:00
commit d79ca646a8
13 arquivos alterados com 49 adições e 40 exclusões
+1 -1
Ver Arquivo
@@ -12,7 +12,7 @@ describe "PanelContainerElement", ->
class TestPanelContainerItemElement extends HTMLElement
createdCallback: ->
@classList.add('test-root')
setModel: (@model) ->
initialize: ({@model}) ->
TestPanelContainerItemElement = document.registerElement 'atom-test-container-item-element', prototype: TestPanelContainerItemElement.prototype
beforeEach ->
+1 -1
Ver Arquivo
@@ -10,7 +10,7 @@ describe "PanelElement", ->
class TestPanelItemElement extends HTMLElement
createdCallback: ->
@classList.add('test-root')
setModel: (@model) ->
initialize: ({@model}) ->
TestPanelItemElement = document.registerElement 'atom-test-item-element', prototype: TestPanelItemElement.prototype
beforeEach ->
+24 -25
Ver Arquivo
@@ -7,11 +7,11 @@ describe "ViewRegistry", ->
beforeEach ->
registry = new ViewRegistry
describe "::getView(object)", ->
describe "::createView(object)", ->
describe "when passed a DOM node", ->
it "returns the given DOM node", ->
node = document.createElement('div')
expect(registry.getView(node)).toBe node
expect(registry.createView(node)).toBe node
describe "when passed a SpacePen view", ->
it "returns the root node of the view with a __spacePenView property pointing at the SpacePen view", ->
@@ -19,20 +19,20 @@ describe "ViewRegistry", ->
@content: -> @div "Hello"
view = new TestView
node = registry.getView(view)
node = registry.createView(view)
expect(node.textContent).toBe "Hello"
expect(node.__spacePenView).toBe view
describe "when passed a model object", ->
describe "when a view provider is registered matching the object's constructor", ->
describe "when the provider has a viewConstructor property", ->
it "constructs a view element and assigns the model on it", ->
it "constructs a view element and calls initialize on it with the creation params", ->
class TestModel
class TestModelSubclass extends TestModel
class TestView
setModel: (@model) ->
initialize: (@params) ->
model = new TestModel
@@ -40,35 +40,37 @@ describe "ViewRegistry", ->
modelConstructor: TestModel
viewConstructor: TestView
view = registry.getView(model)
view = registry.createView(model, a: 1)
expect(view instanceof TestView).toBe true
expect(view.model).toBe model
expect(view.params.a).toBe 1
expect(view.params.model).toBe model
subclassModel = new TestModelSubclass
view2 = registry.getView(subclassModel)
view2 = registry.createView(subclassModel)
expect(view2 instanceof TestView).toBe true
expect(view2.model).toBe subclassModel
expect(view2.params.model).toBe subclassModel
describe "when the provider has a createView method", ->
it "constructs a view element by calling the createView method with the model", ->
it "constructs a view element by calling the createView method with the creation params", ->
class TestModel
class TestView
setModel: (@model) ->
initialize: (@params) ->
registry.addViewProvider
modelConstructor: TestModel
createView: (model) ->
createView: (params) ->
view = new TestView
view.setModel(model)
view.initialize(params)
view
model = new TestModel
view = registry.getView(model)
view = registry.createView(model, a: 1)
expect(view instanceof TestView).toBe true
expect(view.model).toBe model
expect(view.params.a).toBe 1
expect(view.params.model).toBe model
describe "when no view provider is registered for the object's constructor", ->
describe "when the object has a .getViewClass() method", ->
describe "when the object has a .createViewClass() method", ->
it "builds an instance of the view class with the model, then returns its root node with a __spacePenView property pointing at the view", ->
class TestView extends View
@content: (model) -> @div model.name
@@ -79,29 +81,26 @@ describe "ViewRegistry", ->
getViewClass: -> TestView
model = new TestModel("hello")
node = registry.getView(model)
node = registry.createView(model)
expect(node.textContent).toBe "hello"
view = node.__spacePenView
expect(view instanceof TestView).toBe true
expect(view.model).toBe model
# returns the same DOM node for repeated calls
expect(registry.getView(model)).toBe node
describe "when the object has no .getViewClass() method", ->
describe "when the object has no .createViewClass() method", ->
it "throws an exception", ->
expect(-> registry.getView(new Object)).toThrow()
expect(-> registry.createView(new Object)).toThrow()
describe "::addViewProvider(providerSpec)", ->
it "returns a disposable that can be used to remove the provider", ->
class TestModel
class TestView
setModel: (@model) ->
initialize: ->
disposable = registry.addViewProvider
modelConstructor: TestModel
viewConstructor: TestView
expect(registry.getView(new TestModel) instanceof TestView).toBe true
expect(registry.createView(new TestModel) instanceof TestView).toBe true
disposable.dispose()
expect(-> registry.getView(new TestModel)).toThrow()
expect(-> registry.createView(new TestModel)).toThrow()
+1 -1
Ver Arquivo
@@ -462,7 +462,7 @@ describe "Workspace", ->
class TestItemElement extends HTMLElement
constructor: ->
setModel: (@model) ->
initialize: ({@model}) ->
getModel: -> @model
beforeEach ->
+1 -1
Ver Arquivo
@@ -8,7 +8,7 @@ class PaneAxisElement extends HTMLElement
detachedCallback: ->
@subscriptions.dispose()
setModel: (@model) ->
initialize: ({@model}) ->
@subscriptions.add @model.onDidAddChild(@childAdded.bind(this))
@subscriptions.add @model.onDidRemoveChild(@childRemoved.bind(this))
@subscriptions.add @model.onDidReplaceChild(@childReplaced.bind(this))
+1 -1
Ver Arquivo
@@ -11,7 +11,7 @@ class PaneContainerElement extends HTMLElement
PaneContainerView ?= require './pane-container-view'
@__spacePenView = new PaneContainerView(this)
setModel: (@model) ->
initialize: ({@model}) ->
@subscriptions.add @model.observeRoot(@rootChanged.bind(this))
@__spacePenView.setModel(@model)
+3 -3
Ver Arquivo
@@ -43,9 +43,7 @@ class PaneElement extends HTMLElement
createSpacePenShim: ->
@__spacePenView = new PaneView(this)
getModel: -> @model
setModel: (@model) ->
initialize: ({@model}) ->
@subscriptions.add @model.onDidActivate(@activated.bind(this))
@subscriptions.add @model.observeActive(@activeStatusChanged.bind(this))
@subscriptions.add @model.observeActiveItem(@activeItemChanged.bind(this))
@@ -53,6 +51,8 @@ class PaneElement extends HTMLElement
@subscriptions.add @model.onDidDestroy(@paneDestroyed.bind(this))
@__spacePenView.setModel(@model)
getModel: -> @model
activated: ->
@focus()
+1 -1
Ver Arquivo
@@ -6,7 +6,7 @@ class PanelContainerElement extends HTMLElement
getModel: -> @model
setModel: (@model) ->
initialize: ({@model}) ->
@subscriptions.add @model.onDidAddPanel(@panelAdded.bind(this))
@subscriptions.add @model.onDidRemovePanel(@panelRemoved.bind(this))
@subscriptions.add @model.onDidDestroy(@destroyed.bind(this))
+1 -1
Ver Arquivo
@@ -7,7 +7,7 @@ class PanelElement extends HTMLElement
getModel: -> @model
setModel: (@model) ->
initialize: ({@model}) ->
@appendChild(@getItemView())
@classList.add(@model.getClassName().split(' ')...) if @model.getClassName()?
+3
Ver Arquivo
@@ -63,6 +63,9 @@ class TextEditorElement extends HTMLElement
@component.checkForVisibilityChange()
@focus() if @focusOnAttach
initialize: ({model}) ->
@setModel(model)
setModel: (model) ->
throw new Error("Model already assigned on TextEditorElement") if @model?
return if model.isDestroyed()
+1 -1
Ver Arquivo
@@ -63,7 +63,7 @@ class TextEditorView extends View
element = new TextEditorElement
element.lineOverdrawMargin = props?.lineOverdrawMargin
element.setAttribute(name, value) for name, value of attributes if attributes?
element.setModel(model)
element.initialize({model})
return element.__spacePenView
# Handle construction with an element
+10 -3
Ver Arquivo
@@ -1,4 +1,5 @@
{Disposable} = require 'event-kit'
Grim = require 'grim'
# Essential: `ViewRegistry` handles the association between model and view
# types in Atom. We call this association a View Provider. As in, for a given
@@ -121,17 +122,23 @@ class ViewRegistry
@views.set(object, view)
view
createView: (object) ->
createView: (object, params) ->
if object instanceof HTMLElement
object
else if object?.jquery
object[0]?.__spacePenView ?= object
object[0]
else if provider = @findProvider(object)
element = provider.createView?(object)
params ?= {}
params.model = object
element = provider.createView?(params)
unless element?
element = new provider.viewConstructor
element.setModel(object)
if not (typeof element.initialize is 'function') and (typeof element.setModel is 'function')
Grim.deprecate("Define `::initialize` instead of `::setModel` in your view. It will be passed a params hash including the model.")
element.setModel(object)
else
element.initialize(params)
element
else if viewConstructor = object?.getViewClass?()
view = new viewConstructor(object)
+1 -1
Ver Arquivo
@@ -64,7 +64,7 @@ class WorkspaceElement extends HTMLElement
getModel: -> @model
setModel: (@model) ->
initialize: ({@model}) ->
@paneContainer = atom.views.getView(@model.paneContainer)
@verticalAxis.appendChild(@paneContainer)
@addEventListener 'focus', @handleFocus.bind(this)