Skip to content

Commit

Permalink
Don't rely on controller isDirty.
Browse files Browse the repository at this point in the history
By default, we will check if the current route model hasDirtyAttributes.
Resolves #10.
  • Loading branch information
blimmer committed May 15, 2016
1 parent 7b45faa commit c769252
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
7 changes: 6 additions & 1 deletion addon/mixins/confirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export default Mixin.create({
},

isPageDirty() {
return !!get(this, 'controller.isDirty');
const model = this.modelFor(this.routeName);
if (model) {
return !!get(model, 'hasDirtyAttributes');
} else {
return false;
}
},

handleEvent(event) {
Expand Down
5 changes: 0 additions & 5 deletions tests/dummy/app/controllers/bar.js

This file was deleted.

10 changes: 0 additions & 10 deletions tests/dummy/app/controllers/foo.js

This file was deleted.

5 changes: 0 additions & 5 deletions tests/dummy/app/controllers/index.js

This file was deleted.

32 changes: 20 additions & 12 deletions tests/dummy/app/routes/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import Ember from 'ember';
import ConfirmationMixin from 'ember-onbeforeunload/mixins/confirmation';

export default Ember.Route.extend(ConfirmationMixin, {
model() {
return {
id: 1,
username: 'jasonmit'
};
},
confirmationMessage(model) {
return `Unsaved changes for ${model.username}! Are you sure you want to continue?`;
},
onUnload() {
this.set('controller.isDirty', false);
}
model() {
return Ember.Object.create({
id: 1,
username: 'jasonmit',
hasDirtyAttributes: false,
});
},
confirmationMessage(model) {
return `Unsaved changes for ${model.username}! Are you sure you want to continue?`;
},
onUnload() {
const model = this.modelFor(this.routeName);
model.set('hasDirtyAttributes', false);
},
actions: {
markDirty() {
const model = this.modelFor(this.routeName);
model.set('hasDirtyAttributes', true);
}
}
});
33 changes: 33 additions & 0 deletions tests/unit/mixins/confirmation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ describe('ConfirmationMixin', function() {
});
});

describe('isPageDirty', function() {
context('has model', function() {
let subject, modelObj;
beforeEach(function() {
subject = defaultSubject;
modelObj = Ember.Object.create({
hasDirtyAttributes: undefined,
});
sandbox.stub(subject, 'modelFor').returns(modelObj);
});

it('returns hasDirtyAttributes from model', function() {
modelObj.set('hasDirtyAttributes', true);
expect(subject.isPageDirty()).to.be.true;

modelObj.set('hasDirtyAttributes', false);
expect(subject.isPageDirty()).to.be.false;
});
});

context('no modelFor route', function() {
let subject;
beforeEach(function() {
subject = defaultSubject;
sandbox.stub(subject, 'modelFor').returns(undefined);
});

it('returns false', function() {
expect(subject.isPageDirty()).to.be.false;
});
});
});

describe('confirmationMessage', function() {
it('defaults to a sane message', function() {
const subject = defaultSubject;
Expand Down

0 comments on commit c769252

Please sign in to comment.