From 08c1d33836bc90127e655ab41e4ddbc2f1b59529 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 27 Dec 2011 18:21:00 -0600 Subject: [PATCH] Switch Template to use builder. Wire outlets in Template. --- spec/stdlib/template-spec.coffee | 29 ++++++++++++++++++------ spec/stdlib/template/builder-spec.coffee | 10 -------- src/stdlib/template.coffee | 27 ++++++++++++++++++---- src/stdlib/template/builder.coffee | 7 +----- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/spec/stdlib/template-spec.coffee b/spec/stdlib/template-spec.coffee index 1a139ffd0..299a0aa4f 100644 --- a/spec/stdlib/template-spec.coffee +++ b/spec/stdlib/template-spec.coffee @@ -1,20 +1,35 @@ Template = require 'template' -describe "Template", -> +fdescribe "Template", -> describe "toView", -> Foo = null + view = null beforeEach -> class Foo extends Template - content: -> - div -> - h1 @title + content: (attrs) -> + @div => + @h1 attrs.title + @list() + + list: -> + @ol => + @li outlet: 'li1', class: 'foo', "one" + @li outlet: 'li2', class: 'bar', "two" + + view = Foo.build(title: "Zebra") afterEach -> delete window.Foo - it "builds a jquery object based on the content method and extends it with the viewProperties", -> - view = Foo.buildView(title: "Hello World") - expect(view.find('h1').text()).toEqual "Hello World" + describe ".build(attributes)", -> + it "generates markup based on the content method", -> + expect(view).toMatchSelector "div" + expect(view.find("h1:contains(Zebra)")).toExist() + expect(view.find("ol > li.foo:contains(one)")).toExist() + expect(view.find("ol > li.bar:contains(two)")).toExist() + 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/spec/stdlib/template/builder-spec.coffee b/spec/stdlib/template/builder-spec.coffee index 07ed5e2e7..deb3af743 100644 --- a/spec/stdlib/template/builder-spec.coffee +++ b/spec/stdlib/template/builder-spec.coffee @@ -5,16 +5,6 @@ fdescribe "Builder", -> beforeEach -> builder = new Builder - describe ".toFragment()", -> - it "creates outlet references on the fragment for elements with an outlet", -> - builder.tag 'div', -> - builder.tag 'div', id: 'foo', outlet: 'a' - builder.tag 'div', id: 'bar', outlet: 'b' - - fragment = builder.toFragment() - expect(fragment.a).toMatchSelector '#foo' - expect(fragment.b).toMatchSelector '#bar' - describe ".tag(name, args...)", -> it "can generate simple tags", -> builder.tag 'div' diff --git a/src/stdlib/template.coffee b/src/stdlib/template.coffee index 35f358e53..49c3d3197 100644 --- a/src/stdlib/template.coffee +++ b/src/stdlib/template.coffee @@ -1,10 +1,27 @@ $ = require 'jquery' -coffeekup = require 'coffeekup' +_ = require 'underscore' +Builder = require 'template/builder' module.exports = class Template - @buildView: (attributes) -> - (new this).buildView(attributes) + @buildTagMethod: (name) -> + this.prototype[name] = (args...) -> @builder.tag(name, args...) - buildView: (attributes) -> - $(coffeekup.render(@content, attributes)) + _.each(Builder.elements.normal, (name) => @buildTagMethod(name)) + _.each(Builder.elements.void, (name) => @buildTagMethod(name)) + + @build: (attributes) -> + (new this).build(attributes) + + build: (attributes) -> + @builder = new Builder + @content(attributes) + view = @builder.toFragment() + @wireOutlets(view) + view + + wireOutlets: (view) -> + view.find('[outlet]').each -> + elt = $(this) + outletName = elt.attr('outlet') + view[outletName] = elt diff --git a/src/stdlib/template/builder.coffee b/src/stdlib/template/builder.coffee index 304009db1..819842bc7 100644 --- a/src/stdlib/template/builder.coffee +++ b/src/stdlib/template/builder.coffee @@ -25,12 +25,7 @@ class Builder _.map(@document, (x) -> x.toHtml()).join('') toFragment: -> - fragment = $(@toHtml()) - fragment.find('[outlet]').each -> - elt = $(this) - outletName = elt.attr('outlet') - fragment[outletName] = elt - fragment + $(@toHtml()) tag: (name, args...) -> options = @extractOptions(args)