Add Builder.render and instance methods for tags

Builder.render takes a function that calls tag methods and returns a
view fragment.
This commit is contained in:
Danny Greg & Nathan Sobo
2012-01-19 18:30:32 -08:00
parent fe582d7cd7
commit aec88e3404
2 changed files with 26 additions and 1 deletions

View File

@@ -6,6 +6,19 @@ describe "Builder", ->
beforeEach -> builder = new Builder
describe "@render", ->
fit "runs the given function in a fresh builder instance and returns the resulting view fragment", ->
fragment =
Builder.render ->
@div =>
@ol class: 'cool-list', =>
@li()
@li()
expect(fragment).toMatchSelector('div')
expect(fragment.find('ol.cool-list')).toExist()
expect(fragment.find('li').length).toBe 2
describe ".tag(name, args...)", ->
it "can generate simple tags", ->
builder.tag 'div'

View File

@@ -6,6 +6,11 @@ Text = require 'template/text'
module.exports =
class Builder
@render: (fn) ->
builder = new this
fn.call(builder)
builder.toFragment()
@elements:
normal: 'a abbr address article aside audio b bdi bdo blockquote body button
canvas caption cite code colgroup datalist dd del details dfn div dl dt em
@@ -18,6 +23,13 @@ class Builder
void: 'area base br col command embed hr img input keygen link meta param
source track wbr'.split /\s+/
@allElements: ->
@elements.normal.concat(@elements.void)
_.each @allElements(), (tagName) =>
@prototype[tagName] = (args...) -> @tag(tagName, args...)
constructor: ->
@reset()
@@ -37,7 +49,7 @@ class Builder
@document.push(new OpenTag(name, options.attributes))
if @elementIsVoid(name)
if (options.text? or options.content?)
throw new Error("Self-closing tag #{tag} cannot have text or content")
throw new Error("Self-closing tag #{name} cannot have text or content")
else
options.content?()
@text(options.text) if options.text