mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
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:
@@ -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 =>
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user