From 7948543a5b74f480d7a29da0948232d413da13ff Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 27 Dec 2011 18:36:54 -0600 Subject: [PATCH] Template extends view with @viewProperties and calls initialize with attributes passed to build. --- spec/stdlib/template-spec.coffee | 10 ++++++++++ src/stdlib/template.coffee | 3 +++ 2 files changed, 13 insertions(+) diff --git a/spec/stdlib/template-spec.coffee b/spec/stdlib/template-spec.coffee index 299a0aa4f..184fd0cae 100644 --- a/spec/stdlib/template-spec.coffee +++ b/spec/stdlib/template-spec.coffee @@ -17,6 +17,11 @@ fdescribe "Template", -> @li outlet: 'li1', class: 'foo', "one" @li outlet: 'li2', class: 'bar', "two" + viewProperties: + initialize: (attrs) -> + @initializeCalledWith = attrs + foo: "bar" + view = Foo.build(title: "Zebra") afterEach -> @@ -29,6 +34,11 @@ fdescribe "Template", -> expect(view.find("ol > li.foo:contains(one)")).toExist() expect(view.find("ol > li.bar:contains(two)")).toExist() + it "extends the view with viewProperties, calling the 'constructor' property if present", -> + expect(view.constructor).toBeDefined() + expect(view.foo).toBe("bar") + expect(view.initializeCalledWith).toEqual(title: "Zebra") + it "wires references for elements with 'outlet' attributes", -> expect(view.li1).toMatchSelector("li.foo:contains(one)") expect(view.li2).toMatchSelector("li.bar:contains(two)") diff --git a/src/stdlib/template.coffee b/src/stdlib/template.coffee index 49c3d3197..cb4f7a31f 100644 --- a/src/stdlib/template.coffee +++ b/src/stdlib/template.coffee @@ -18,6 +18,9 @@ class Template @content(attributes) view = @builder.toFragment() @wireOutlets(view) + if @viewProperties + $.extend(view, @viewProperties) + view.initialize?(attributes) view wireOutlets: (view) ->