diff --git a/spec/stdlib/template-spec.coffee b/spec/stdlib/template-spec.coffee
index e9828ec21..477990014 100644
--- a/spec/stdlib/template-spec.coffee
+++ b/spec/stdlib/template-spec.coffee
@@ -91,26 +91,31 @@ describe "Template", ->
expect(view.subview.header.view()).toBe view.subview
describe "when a view is inserted within another element with jquery", ->
- attachHandler = null
+ [attachHandler, subviewAttachHandler] = []
beforeEach ->
attachHandler = jasmine.createSpy 'attachHandler'
+ subviewAttachHandler = jasmine.createSpy 'subviewAttachHandler'
view.on 'attach', attachHandler
+ view.subview.on 'attach', subviewAttachHandler
describe "when attached to an element that is on the DOM", ->
- it "triggers the 'attach' event on the view", ->
+ it "triggers an 'attach' event on the view and its subviews", ->
content = $('#jasmine-content')
content.append view
expect(attachHandler).toHaveBeenCalled()
+ expect(subviewAttachHandler).toHaveBeenCalled()
view.detach()
content.empty()
attachHandler.reset()
+ subviewAttachHandler.reset()
otherElt = $('
')
content.append(otherElt)
view.insertBefore(otherElt)
expect(attachHandler).toHaveBeenCalled()
+ expect(subviewAttachHandler).toHaveBeenCalled()
describe "when attached to an element that is not on the DOM", ->
it "does not trigger an attach event", ->
diff --git a/src/stdlib/template.coffee b/src/stdlib/template.coffee
index 9e4047a72..1d6cda611 100644
--- a/src/stdlib/template.coffee
+++ b/src/stdlib/template.coffee
@@ -27,7 +27,7 @@ class Template
@bindEvents(view)
if @viewProperties
$.extend(view, @viewProperties)
- view.data('triggerAttach', true)
+ view.attr('triggerAttachEvents', true)
view.initialize?(attributes)
view
@@ -53,21 +53,21 @@ $.fn.view = ->
this.data('view')
# Trigger attach event when views are added to the DOM
-checkIfAttached = (elt) ->
- if elt.data?('triggerAttach') and elt.parents('html').length
- elt.trigger('attach')
+triggerAttachEvent = (elt) ->
+ if elt.attr?('triggerAttachEvents') and elt.parents('html').length
+ elt.find('[triggerAttachEvents]').add(elt).trigger('attach')
_.each ['append', 'prepend', 'after', 'before'], (methodName) ->
originalMethod = $.fn[methodName]
$.fn[methodName] = (args...) ->
result = originalMethod.apply(this, args)
- checkIfAttached(args[0])
+ triggerAttachEvent(args[0])
result
_.each ['prependTo', 'appendTo', 'insertAfter', 'insertBefore'], (methodName) ->
originalMethod = $.fn[methodName]
$.fn[methodName] = (args...) ->
result = originalMethod.apply(this, args)
- checkIfAttached(this)
+ triggerAttachEvent(this)
result