mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2025-01-08 16:19:17 +03:00
Allow view providers to specify a createView factory method
If present, it will be called with the model object instead of instantiating the view constructor directly and assigning a model on it. This gives users more flexibility when constructing views.
This commit is contained in:
parent
74d772f069
commit
d344adc21e
@ -49,6 +49,24 @@ describe "ViewRegistry", ->
|
||||
expect(view2 instanceof TestView).toBe true
|
||||
expect(view2.model).toBe subclassModel
|
||||
|
||||
describe "when the provider has a createView method", ->
|
||||
it "constructs a view element by calling the createView method with the model", ->
|
||||
class TestModel
|
||||
class TestView
|
||||
setModel: (@model) ->
|
||||
|
||||
registry.addViewProvider
|
||||
modelConstructor: TestModel
|
||||
createView: (model) ->
|
||||
view = new TestView
|
||||
view.setModel(model)
|
||||
view
|
||||
|
||||
model = new TestModel
|
||||
view = registry.getView(model)
|
||||
expect(view instanceof TestView).toBe true
|
||||
expect(view.model).toBe model
|
||||
|
||||
describe "when no view provider is registered for the object's constructor", ->
|
||||
describe "when the object has a .getViewClass() 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", ->
|
||||
|
@ -29,8 +29,10 @@ class ViewRegistry
|
||||
object[0].__spacePenView ?= object
|
||||
object[0]
|
||||
else if provider = @findProvider(object)
|
||||
element = new provider.viewConstructor
|
||||
element.setModel(object)
|
||||
element = provider.createView?(object)
|
||||
unless element?
|
||||
element = new provider.viewConstructor
|
||||
element.setModel(object)
|
||||
element
|
||||
else if viewConstructor = object?.getViewClass?()
|
||||
view = new viewConstructor(object)
|
||||
|
Loading…
Reference in New Issue
Block a user