Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new option, closeOn, to override $locationChangeSuccess. #215

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ The `showModal` function takes an object with these fields:
* `scope`: Optional. If provided, the modal controller will use a new scope as a child of `scope` (created by calling `scope.$new()`) rather than a new scope created as a child of `$rootScope`.
* `bodyClass`: Optional. The custom css class to append to the body while the modal is open (optional, useful when not using Bootstrap).
* `preClose`: Optional. A function which will be called before the process of closing a modal starts. The signature is `function preClose(modal, result, delay)`. It is provided the `modal` object, the `result` which was passed to `close` and the `delay` which was passed to close.
* `closeOn`: A $rootScope event which will close the modal. The default value is `$locationChangeSuccess`. A value of `false` can be passed instead of an event to turn this feature off.

#### The Modal Object

Expand Down
14 changes: 10 additions & 4 deletions dst/angular-modal-service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dst/angular-modal-service.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dst/angular-modal-service.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dst/angular-modal-service.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/angular-modal-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ module.factory('ModalService', ['$animate', '$document', '$compile', '$controlle

// The main modal object we will build.
var modal = {};
var rootScopeOnClose;

// Create a new scope for the modal.
var modalScope = (options.scope || $rootScope).$new();
var rootScopeOnClose = $rootScope.$on('$locationChangeSuccess', cleanUpClose);

!options.hasOwnProperty('closeOn') && (options.closeOn = '$locationChangeSuccess');

if (options.closeOn !== false) {
rootScopeOnClose = $rootScope.$on(options.closeOn, cleanUpClose);
}

// Create the inputs object to the controller - this will include
// the scope, as well as all inputs provided.
Expand Down Expand Up @@ -184,7 +190,7 @@ module.factory('ModalService', ['$animate', '$document', '$compile', '$controlle
});

// remove event watcher
rootScopeOnClose && rootScopeOnClose();
!!rootScopeOnClose && rootScopeOnClose();
}

})
Expand Down
30 changes: 27 additions & 3 deletions test/dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,47 @@ describe('dom', () => {

});

it('should remove the template html from the dom when the $locationChangeSuccess event is fired', () => {
it('should remove the template html from the dom when the onClose $rootScope event is fired', () => {

$httpBackend.expectGET('some/template2.html');

ModalService.showModal({
controller: "DomController",
templateUrl: "some/template2.html"
templateUrl: "some/template2.html",
closeOn: "$locationChangeSuccess"
}).then((modal) => {

// We should be able to find the element that has been created in the dom.
expect(document.getElementById('template2')).not.toBeNull();
$rootScope.$emit('$locationChangeSuccess');
expect(document.getElementById('template2')).toBeNull();

});

$httpBackend.flush();
$timeout.flush();

});

it('should NOT remove the template html from the dom if false is passed to onClose', () => {

$httpBackend.expectGET('some/template2.html');

ModalService.showModal({
controller: "DomController",
templateUrl: "some/template2.html",
closeOn: false
}).then((modal) => {

// We should be able to find the element that has been created in the dom.
expect(document.getElementById('template2')).not.toBeNull();
$rootScope.$emit('$locationChangeSuccess');
expect(document.getElementById('template2')).not.toBeNull();

modal.close.then((result) => {
expect(document.getElementById('template2')).toBeNull();
});

$rootScope.$emit('$locationChangeSuccess');
});

$httpBackend.flush();
Expand Down