mirror of
https://github.com/atom/atom.git
synced 2026-01-24 06:18:03 -05:00
Switch Template to use builder. Wire outlets in Template.
This commit is contained in:
@@ -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)")
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user