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

[CLEANUP DEPRECATION errors] deprecates use of Ember.Evented with DS.Errors #5663

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 3 additions & 3 deletions addon/-legacy-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1228,15 +1228,15 @@ export default class InternalModel {
}

addErrorMessageToAttribute(attribute, message) {
get(this.getRecord(), 'errors')._add(attribute, message);
get(this.getRecord(), 'errors').add(attribute, message);
}

removeErrorMessageFromAttribute(attribute) {
get(this.getRecord(), 'errors')._remove(attribute);
get(this.getRecord(), 'errors').remove(attribute);
}

clearErrorMessages() {
get(this.getRecord(), 'errors')._clear();
get(this.getRecord(), 'errors').clear();
}

hasErrors() {
Expand Down
13 changes: 1 addition & 12 deletions addon/-legacy-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,18 +396,7 @@ const Model = EmberObject.extend(Evented, {
@type {DS.Errors}
*/
errors: computed(function() {
let errors = Errors.create();

errors._registerHandlers(
this._internalModel,
function() {
this.send('becameInvalid');
},
function() {
this.send('becameValid');
}
);
return errors;
return Errors.create();
}).readOnly(),

/**
Expand Down
45 changes: 45 additions & 0 deletions addon/-private/deprecated-evented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Mixin from '@ember/object/mixin';
import Evented from '@ember/object/evented';
import { deprecate } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

let DeprecatedEvented = Evented;

if (DEBUG) {
const printDeprecation = function(ctx) {
deprecate(
`Use of event functionality provided by Ember.Evented with ${
ctx._debugContainerKey
} has been deprecated.`,
{
id: 'ember-data:no-longer-evented',
until: '3.8',
}
);
};

DeprecatedEvented = Mixin.create(Evented, {
has() {
printDeprecation(this);
return this._super(...arguments);
},
off() {
printDeprecation(this);
return this._super(...arguments);
},
on() {
printDeprecation(this);
return this._super(...arguments);
},
one() {
printDeprecation(this);
return this._super(...arguments);
},
trigger() {
printDeprecation(this);
return this._super(...arguments);
},
});
}

export default DeprecatedEvented;
97 changes: 7 additions & 90 deletions addon/-private/system/model/errors.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { mapBy, not } from '@ember/object/computed';
import Evented from '@ember/object/evented';
import DeprecatedEvented from '../../deprecated-evented';
import ArrayProxy from '@ember/array/proxy';
import { set, get, computed } from '@ember/object';
import { makeArray, A } from '@ember/array';
import MapWithDefault from '../map-with-default';
import { warn } from '@ember/debug';

/**
@module ember-data
Expand Down Expand Up @@ -85,18 +84,7 @@ import { warn } from '@ember/debug';
@uses Ember.Enumerable
@uses Ember.Evented
*/
export default ArrayProxy.extend(Evented, {
/**
Register with target handler

@method _registerHandlers
@private
*/
_registerHandlers(target, becameInvalid, becameValid) {
this.on('becameInvalid', target, becameInvalid);
this.on('becameValid', target, becameValid);
},

export default ArrayProxy.extend(DeprecatedEvented, {
/**
@property errorsByAttributeName
@type {MapWithDefault}
Expand Down Expand Up @@ -186,8 +174,7 @@ export default ArrayProxy.extend(Evented, {
isEmpty: not('length').readOnly(),

/**
Adds error messages to a given attribute and sends
`becameInvalid` event to the record.
Adds error messages to a given attribute

Example:

Expand All @@ -200,31 +187,11 @@ export default ArrayProxy.extend(Evented, {
@method add
@param {String} attribute
@param {(Array|String)} messages
@deprecated
*/
add(attribute, messages) {
warn(`Interacting with a record errors object will no longer change the record state.`, false, {
id: 'ds.errors.add',
});

let wasEmpty = get(this, 'isEmpty');

this._add(attribute, messages);

if (wasEmpty && !get(this, 'isEmpty')) {
this.trigger('becameInvalid');
}
},

/**
Adds error messages to a given attribute without sending event.

@method _add
@private
*/
_add(attribute, messages) {
messages = this._findOrCreateMessages(attribute, messages);
this.addObjects(messages);

get(this, 'errorsByAttributeName')
.get(attribute)
.addObjects(messages);
Expand Down Expand Up @@ -258,8 +225,7 @@ export default ArrayProxy.extend(Evented, {
},

/**
Removes all error messages from the given attribute and sends
`becameValid` event to the record if there no more errors left.
Removes all error messages from the given attribute

Example:

Expand Down Expand Up @@ -290,35 +256,8 @@ export default ArrayProxy.extend(Evented, {

@method remove
@param {String} attribute
@deprecated
*/
remove(attribute) {
warn(`Interacting with a record errors object will no longer change the record state.`, false, {
id: 'ds.errors.remove',
});

if (get(this, 'isEmpty')) {
return;
}

this._remove(attribute);

if (get(this, 'isEmpty')) {
this.trigger('becameValid');
}
},

/**
Removes all error messages from the given attribute without sending event.

@method _remove
@private
*/
_remove(attribute) {
if (get(this, 'isEmpty')) {
return;
}

let content = this.rejectBy('attribute', attribute);
set(this, 'content', content);
get(this, 'errorsByAttributeName').delete(attribute);
Expand All @@ -328,8 +267,7 @@ export default ArrayProxy.extend(Evented, {
},

/**
Removes all error messages and sends `becameValid` event
to the record.
Removes all error messages.

Example:

Expand All @@ -347,29 +285,8 @@ export default ArrayProxy.extend(Evented, {
```

@method clear
@deprecated
*/
clear() {
warn(`Interacting with a record errors object will no longer change the record state.`, false, {
id: 'ds.errors.clear',
});

if (get(this, 'isEmpty')) {
return;
}

this._clear();
this.trigger('becameValid');
},

/**
Removes all error messages.
to the record.

@method _clear
@private
*/
_clear() {
if (get(this, 'isEmpty')) {
return;
}
Expand All @@ -386,7 +303,7 @@ export default ArrayProxy.extend(Evented, {
this.notifyPropertyChange(attribute);
}, this);

ArrayProxy.prototype.clear.call(this);
this._super();
},

/**
Expand Down
6 changes: 3 additions & 3 deletions addon/-record-data-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1156,15 +1156,15 @@ export default class InternalModel {
}

addErrorMessageToAttribute(attribute, message) {
get(this.getRecord(), 'errors')._add(attribute, message);
get(this.getRecord(), 'errors').add(attribute, message);
}

removeErrorMessageFromAttribute(attribute) {
get(this.getRecord(), 'errors')._remove(attribute);
get(this.getRecord(), 'errors').remove(attribute);
}

clearErrorMessages() {
get(this.getRecord(), 'errors')._clear();
get(this.getRecord(), 'errors').clear();
}

hasErrors() {
Expand Down
13 changes: 1 addition & 12 deletions addon/-record-data-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,7 @@ const Model = EmberObject.extend(Evented, {
@type {DS.Errors}
*/
errors: computed(function() {
let errors = Errors.create();

errors._registerHandlers(
this._internalModel,
function() {
this.send('becameInvalid');
},
function() {
this.send('becameValid');
}
);
return errors;
return Errors.create();
}).readOnly(),

/**
Expand Down
41 changes: 16 additions & 25 deletions tests/integration/adapter/store-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ test('if a deleted record errors, it enters the error state', function(assert) {
});
});

test('if a created record is marked as invalid by the server, it enters an error state', function(assert) {
test('if a created record is marked as invalid by the server, it enters an error state', async function(assert) {
adapter.createRecord = function(store, type, snapshot) {
assert.equal(type, Person, 'the type is correct');

Expand All @@ -537,37 +537,28 @@ test('if a created record is marked as invalid by the server, it enters an error
};

let yehuda = store.createRecord('person', { id: 1, name: 'Yehuda Katz' });

// Wrap this in an Ember.run so that all chained async behavior is set up
// before flushing any scheduled behavior.
return run(function() {
return yehuda
.save()
.catch(error => {
assert.equal(get(yehuda, 'isValid'), false, 'the record is invalid');
assert.ok(get(yehuda, 'errors.name'), 'The errors.name property exists');
await yehuda.save().catch(error => {
assert.equal(get(yehuda, 'isValid'), false, 'the record is invalid');
assert.ok(get(yehuda, 'errors.name'), 'The errors.name property exists');

set(yehuda, 'updatedAt', true);
assert.equal(get(yehuda, 'isValid'), false, 'the record is still invalid');
set(yehuda, 'updatedAt', true);
assert.equal(get(yehuda, 'isValid'), false, 'the record is still invalid');

set(yehuda, 'name', 'Brohuda Brokatz');
set(yehuda, 'name', 'Brohuda Brokatz');

assert.equal(
get(yehuda, 'isValid'),
true,
'the record is no longer invalid after changing'
);
assert.equal(get(yehuda, 'hasDirtyAttributes'), true, 'the record has outstanding changes');

assert.equal(get(yehuda, 'isNew'), true, 'precond - record is still new');
assert.equal(get(yehuda, 'isValid'), true, 'the record is no longer invalid after changing');
assert.equal(get(yehuda, 'hasDirtyAttributes'), true, 'the record has outstanding changes');
assert.equal(get(yehuda, 'isNew'), true, 'precond - record is still new');
});

return yehuda.save();
})
.then(person => {
assert.strictEqual(person, yehuda, 'The promise resolves with the saved record');
await yehuda.save().then(person => {
assert.strictEqual(person, yehuda, 'The promise resolves with the saved record');

assert.equal(get(yehuda, 'isValid'), true, 'record remains valid after committing');
assert.equal(get(yehuda, 'isNew'), false, 'record is no longer new');
});
assert.equal(get(yehuda, 'isValid'), true, 'record remains valid after committing');
assert.equal(get(yehuda, 'isNew'), false, 'record is no longer new');
});
});

Expand Down