Permalink
Browse files

Merge pull request #7 from metaskills/bug-attach-args

Fix DOM manipulation bugs for append, prepend, after and before.
  • Loading branch information...
2 parents e2ad9fe + abe171a commit e9ea10591a6760415c22a6521181ba0eb2c105ed @nathansobo nathansobo committed Apr 5, 2012
Showing with 33 additions and 6 deletions.
  1. +29 −3 space-pen-spec.coffee
  2. +4 −3 space-pen.coffee
View
@@ -91,20 +91,25 @@ describe "View", ->
expect(view.subview.header.view()).toBe view.subview
describe "when a view is inserted within another element with jquery", ->
- [attachHandler, subviewAttachHandler] = []
+ [content, attachHandler, subviewAttachHandler] = []
beforeEach ->
attachHandler = jasmine.createSpy 'attachHandler'
subviewAttachHandler = jasmine.createSpy 'subviewAttachHandler'
view.on 'attach', attachHandler
view.subview.on 'attach', subviewAttachHandler
+
+ it "accepts undefined arguments as jQuery does", ->
+ view.append undefined
describe "when attached to an element that is on the DOM", ->
+ beforeEach ->
+ content = $('#jasmine-content')
+
afterEach ->
- $('#jasmine-content').empty()
+ content.empty()
it "triggers an 'attach' event on the view and its subviews", ->
- content = $('#jasmine-content')
content.append view
expect(attachHandler).toHaveBeenCalled()
expect(subviewAttachHandler).toHaveBeenCalled()
@@ -120,6 +125,27 @@ describe "View", ->
expect(attachHandler).toHaveBeenCalled()
expect(subviewAttachHandler).toHaveBeenCalled()
+ describe "with multiple arguments", ->
+ [view2, view3, view2Handler, view3Handler] = []
+
+ beforeEach ->
+ view2Class = class extends View
+ @content: -> @div id: "view2"
+ view3Class = class extends View
+ @content: -> @div id: "view3"
+ view2 = new view2Class
+ view3 = new view3Class
+ view2Handler = jasmine.createSpy 'view2Handler'
+ view3Handler = jasmine.createSpy 'view3Handler'
+ view2.on 'attach', view2Handler
+ view3.on 'attach', view3Handler
+
+ it "triggers an 'attach' event on all args", ->
+ content.append view, [view2, view3]
+ expect(attachHandler).toHaveBeenCalled()
+ expect(view2Handler).toHaveBeenCalled()
+ expect(view3Handler).toHaveBeenCalled()
+
describe "when attached to an element that is not on the DOM", ->
it "does not trigger an attach event", ->
fragment = $('<div>')
View
@@ -160,15 +160,16 @@ jQuery.fn.view = -> this.data('view')
# Trigger attach event when views are added to the DOM
triggerAttachEvent = (element) ->
- if element.attr?('triggerAttachEvents') and element.parents('html').length
+ if element?.attr?('triggerAttachEvents') and element.parents('html').length
element.find('[triggerAttachEvents]').add(element).trigger('attach')
for methodName in ['append', 'prepend', 'after', 'before']
do (methodName) ->
originalMethod = $.fn[methodName]
jQuery.fn[methodName] = (args...) ->
- result = originalMethod.apply(this, args)
- triggerAttachEvent(args[0])
+ flatArgs = [].concat args...
+ result = originalMethod.apply(this, flatArgs)
+ triggerAttachEvent arg for arg in flatArgs
result
for methodName in ['prependTo', 'appendTo', 'insertAfter', 'insertBefore']

0 comments on commit e9ea105

Please sign in to comment.