Builder has class methods for every tag

Shorthand: If you know the top-level tag you want to render, you can
call `Builder.div class: "foo", -> ...` instead of calling render.
This commit is contained in:
Danny Greg & Nathan Sobo
2012-01-19 18:54:09 -08:00
parent aec88e3404
commit 0e9bfe4d1a
2 changed files with 27 additions and 3 deletions

View File

@@ -6,8 +6,20 @@ describe "Builder", ->
beforeEach -> builder = new Builder
describe "tag class methods", ->
it "calls render, assuming the arguments to the current method as the first tag", ->
fragment =
Builder.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 "@render", ->
fit "runs the given function in a fresh builder instance and returns the resulting view fragment", ->
it "runs the given function in a fresh builder instance and returns the resulting view fragment", ->
fragment =
Builder.render ->
@div =>

View File

@@ -23,13 +23,25 @@ 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) =>
@buildTagClassMethod: (tagName) ->
this[tagName] = (args...) ->
@render ->
argsWithBoundFunctions = args.map (arg) =>
if _.isFunction(arg)
_.bind(arg, this)
else
arg
@tag(tagName, argsWithBoundFunctions...)
@buildTagInstanceMethod: (tagName) ->
@prototype[tagName] = (args...) -> @tag(tagName, args...)
@allElements().forEach (tagName) => @buildTagClassMethod(tagName)
@allElements().forEach (tagName) => @buildTagInstanceMethod(tagName)
constructor: ->
@reset()