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

Add onDestroy callback method for flash object destroy #211

Merged
Merged
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ Ember.get(this, 'flashMessages').add({
sticky: true,
showProgress: true,
extendedTimeout: 500,
destroyOnClick: false
destroyOnClick: false,
onDestroy() {
// behavior triggered when flash is destroyed
}
});

Ember.get(this, 'flashMessages').success('This is amazing', {
Expand Down Expand Up @@ -144,6 +147,12 @@ Ember.get(this, 'flashMessages').success('This is amazing', {

By default, flash messages will be destroyed on click. Disabling this can be useful if the message supports user interaction.

- `onDestroy: function`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind also adding an example of this on line 90?


Default: `undefined`

A function to be called when the flash message is destroyed.

### Arbitrary options
You can also add arbitrary options to messages:

Expand Down
6 changes: 6 additions & 0 deletions addon/flash/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export default EmberObject.extend(Evented, {
willDestroy() {
this._cancelAllTimers();

const onDestroy = get(this, 'onDestroy');

if (onDestroy) {
onDestroy();
}

this._super(...arguments);
},

Expand Down
15 changes: 15 additions & 0 deletions tests/unit/flash/object-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,18 @@ test('#resumeTimers resets timers', function(assert) {

assert.notOk(setupTimerStub.called, 'setup timers is not called');
});

test('it calls `onDestroy` when object is destroyed', function(assert) {
assert.expect(1);

const callbackFlash = FlashMessage.create({
extendedTimeout: 1000,
onDestroy() {
assert.ok(true, 'onDestroy is called');
}
});

run(() => {
callbackFlash.destroyMessage();
});
});