Skip to content
This repository was archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
fix(modal): Fixed mac-modal directive not cleaning up when being remo…
Browse files Browse the repository at this point in the history
…ved from DOM
  • Loading branch information
adrianlee44 committed Feb 8, 2014
1 parent 29ac521 commit f2b3efb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/directives/modal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ angular.module("Mac").directive("macModal", [
element[0].getElementsByClassName "modal-content-wrapper"
).replaceWith clone

opts = util.extendAttributes "macModal", modalViews.defaults, attrs
opts = util.extendAttributes "macModal", modalViews.defaults, attrs
regId = null

if opts.overlayClose
element.on "click", ($event) ->
Expand All @@ -39,13 +40,18 @@ angular.module("Mac").directive("macModal", [

registerModal = (id) ->
if id? and id
regId = id
opts.callback = $parse(attrs.macModalOpen)($scope)
modal.register id, element, opts

if attrs.id
registerModal attrs.id
else
attrs.$observe "macModal", (id) -> registerModal id

# NOTE: Remove from modal service when mac-modal directive is removed
# from DOM
$scope.$on "$destroy", -> modal.unregister regId if regId
]).

#
Expand Down
20 changes: 20 additions & 0 deletions test/unit/modal.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ describe "Mac modal", ->

expect(modal.registered["test-modal"]).toBeDefined()

it "should unregister the modal when $scope is destroyed", ->
modalElement = $compile("<mac-modal id='test-modal'></mac-modal>") $rootScope
$rootScope.$digest()

$rootScope.$destroy()

expect(modal.registered["test-modal"]).not.toBeDefined()

it "should unregister the correct one", ->
scope = $rootScope.$new()
scope1 = $rootScope.$new()
modalElement = $compile("<mac-modal id='test-modal'></mac-modal>") scope
modalElement = $compile("<mac-modal id='test-modal-1'></mac-modal>") scope1
$rootScope.$digest()

scope.$destroy()

expect(modal.registered["test-modal"]).not.toBeDefined()
expect(modal.registered["test-modal-1"]).toBeDefined()

it "should close the modal was 'escape' key", ->
opened = false
modalElement = $compile("<mac-modal id='test-modal' mac-modal-keyboard></mac-modal>") $rootScope
Expand Down

0 comments on commit f2b3efb

Please sign in to comment.