Fix bug where subviews could clobber outlets on superviews.

Now we wire outlets before the subviews are attached, preventing any outlets on the subview from overwriting outlets on the superview.
This commit is contained in:
Corey Johnson & Nathan Sobo 2011-12-29 16:29:06 -06:00
parent 2eb7d2a215
commit b279063550
3 changed files with 13 additions and 9 deletions

View File

@ -8,13 +8,13 @@ describe "Template", ->
subviewTemplate = class extends Template
content: (params) ->
@div =>
@h2 params.title
@h2 { outlet: "header" }, params.title
@div "I am a subview"
template = class extends Template
content: (attrs) ->
@div =>
@h1 attrs.title
@h1 { outlet: 'header' }, attrs.title
@list()
@subview 'subview', subviewTemplate.build(title: "Subview")
@ -52,6 +52,10 @@ describe "Template", ->
expect(view.subview).toExist()
expect(view.subview.find('h2:contains(Subview)')).toExist()
it "does not overwrite outlets on the superview with outlets from the subviews", ->
expect(view.header).toMatchSelector "h1"
expect(view.subview.header).toMatchSelector "h2"
it "binds events for elements with event name attributes", ->
spyOn(view, 'li1Clicked').andCallFake (event, elt) ->
expect(event.type).toBe 'click'

View File

@ -24,7 +24,6 @@ class Template
@builder = new Builder
@content(attributes)
view = @builder.toFragment()
@wireOutlets(view)
@bindEvents(view)
if @viewProperties
$.extend(view, @viewProperties)
@ -39,12 +38,6 @@ class Template
subview: (args...) ->
@builder.subview.apply(@builder, args)
wireOutlets: (view) ->
view.find('[outlet]').each ->
elt = $(this)
outletName = elt.attr('outlet')
view[outletName] = elt
bindEvents: (view) ->
for eventName in this.constructor.events
view.find("[#{eventName}]").each ->

View File

@ -26,6 +26,7 @@ class Builder
toFragment: ->
fragment = $(@toHtml())
@wireOutlets fragment
fn(fragment) for fn in @postProcessingFns
fragment
@ -63,6 +64,12 @@ class Builder
text: (string) ->
@document.push(new Text(string))
wireOutlets: (view) ->
view.find('[outlet]').each ->
elt = $(this)
outletName = elt.attr('outlet')
view[outletName] = elt
reset: ->
@document = []
@postProcessingFns = []