mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Merge branch 'master' into editor
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
$ = require 'jquery'
|
||||
Template = require 'template'
|
||||
|
||||
describe "Template", ->
|
||||
@@ -90,3 +91,31 @@ describe "Template", ->
|
||||
expect(view.subview.view()).toBe view.subview
|
||||
expect(view.subview.header.view()).toBe view.subview
|
||||
|
||||
describe "when a view is inserted within another element with jquery", ->
|
||||
attachHandler = null
|
||||
|
||||
beforeEach ->
|
||||
attachHandler = jasmine.createSpy 'attachHandler'
|
||||
view.on 'attach', attachHandler
|
||||
|
||||
describe "when attached to an element that is on the DOM", ->
|
||||
it "triggers the 'attach' event on the view", ->
|
||||
content = $('#jasmine-content')
|
||||
content.append view
|
||||
expect(attachHandler).toHaveBeenCalled()
|
||||
|
||||
view.detach()
|
||||
content.empty()
|
||||
attachHandler.reset()
|
||||
|
||||
otherElt = $('<div>')
|
||||
content.append(otherElt)
|
||||
view.insertBefore(otherElt)
|
||||
expect(attachHandler).toHaveBeenCalled()
|
||||
|
||||
describe "when attached to an element that is not on the DOM", ->
|
||||
it "does not trigger an attach event", ->
|
||||
fragment = $('<div>')
|
||||
fragment.append view
|
||||
expect(attachHandler).not.toHaveBeenCalled()
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ class Template
|
||||
@bindEvents(view)
|
||||
if @viewProperties
|
||||
$.extend(view, @viewProperties)
|
||||
view.data('triggerAttach', true)
|
||||
view.initialize?(attributes)
|
||||
view
|
||||
|
||||
@@ -51,3 +52,22 @@ class Template
|
||||
$.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')
|
||||
|
||||
_.each ['append', 'prepend', 'after', 'before'], (methodName) ->
|
||||
originalMethod = $.fn[methodName]
|
||||
$.fn[methodName] = (args...) ->
|
||||
result = originalMethod.apply(this, args)
|
||||
checkIfAttached(args[0])
|
||||
result
|
||||
|
||||
_.each ['prependTo', 'appendTo', 'insertAfter', 'insertBefore'], (methodName) ->
|
||||
originalMethod = $.fn[methodName]
|
||||
$.fn[methodName] = (args...) ->
|
||||
result = originalMethod.apply(this, args)
|
||||
checkIfAttached(this)
|
||||
result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user