From d344adc21e4bf724f539a164d57a48bb4e093b00 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 19 Sep 2014 15:25:35 -0600 Subject: [PATCH] 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. --- spec/view-registry-spec.coffee | 18 ++++++++++++++++++ src/view-registry.coffee | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spec/view-registry-spec.coffee b/spec/view-registry-spec.coffee index 87933a241..b2cf25629 100644 --- a/spec/view-registry-spec.coffee +++ b/spec/view-registry-spec.coffee @@ -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", -> diff --git a/src/view-registry.coffee b/src/view-registry.coffee index a3598791d..5eb9a9458 100644 --- a/src/view-registry.coffee +++ b/src/view-registry.coffee @@ -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)