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:
Nathan Sobo 2014-09-19 15:25:35 -06:00
parent 74d772f069
commit d344adc21e
2 changed files with 22 additions and 2 deletions

View File

@ -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", ->

View File

@ -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)