Skip to content

Commit

Permalink
Merge remote-tracking branch 'emberjs/master' into friendly-errors
Browse files Browse the repository at this point in the history
Conflicts:
	addon/adapters/rest-adapter.js
	tests/integration/adapter/rest-adapter-test.js
  • Loading branch information
nikz committed Dec 4, 2015
2 parents 75d311c + dc0be67 commit dd1ac98
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 7 deletions.
12 changes: 7 additions & 5 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import Ember from "ember";
@main ember-data
*/

if (Ember.VERSION.match(/^1\.[0-7]\./)) {
throw new Ember.Error("Ember Data requires at least Ember 1.8.0, but you have " +
if (Ember.VERSION.match(/^1\.([0-9]|1[0-2])\./)) {
throw new Ember.Error("Ember Data requires at least Ember 1.13.0, but you have " +
Ember.VERSION +
". Please upgrade your version of Ember, then upgrade Ember Data");
". Please upgrade your version of Ember, then upgrade Ember Data.");
}

if (Ember.VERSION.match(/^1\.12\.0/)) {
throw new Ember.Error("Ember Data does not work with Ember 1.12.0. Please upgrade to Ember 1.12.1 or higher.");
if (Ember.VERSION.match(/^1\.13\./)) {
Ember.warn(`Use of Ember Data 2+ with Ember 1.13 is unsupported. Please upgrade your version of Ember to 2.0 or higher.`, false, {
id: 'ds.version.ember-1-13'
});
}

import DS from "ember-data/core";
Expand Down
1 change: 1 addition & 0 deletions addon/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ export default Serializer.extend({
the payload and just sends the raw serialized JSON object.
If your server expects namespaced keys, you should consider using the RESTSerializer.
Otherwise you can override this method to customize how the record is added to the hash.
The hash property should be modified by reference.
For example, your server may expect underscored root objects.
Expand Down
1 change: 1 addition & 0 deletions addon/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ var RESTSerializer = JSONSerializer.extend({

/**
You can use this method to customize the root keys serialized into the JSON.
The hash property should be modified by reference (possibly using something like _.extend)
By default the REST Serializer sends the modelName of a model, which is a camelized
version of the name.
Expand Down
4 changes: 2 additions & 2 deletions addon/system/model/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var MapWithDefault = Ember.MapWithDefault;
user.save();
```
Your backend data store might return a response that looks like
this. This response will be used to populate the error object.
Your backend data store might return a response with status code 422 (Unprocessable Entity)
and that looks like this. This response will be used to populate the error object.
```javascript
{
Expand Down
157 changes: 157 additions & 0 deletions packages/ember-data/tests/integration/records/error-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
var env, store, Person;
var attr = DS.attr;
var run = Ember.run;

module('integration/records/error', {
setup: function() {
Person = DS.Model.extend({
firstName: attr('string'),
lastName: attr('string')
});
Person.toString = function() { return 'Person'; };

env = setupStore({
person: Person
});

store = env.store;
},

teardown: function() {
Ember.run(function() {
env.container.destroy();
});
}
});

test('adding errors during root.loaded.created.invalid works', function() {
expect(3);

var person = run(() => {
store.push({
data: {
type: 'person',
id: 'wat',
attributes: {
firstName: 'Yehuda',
lastName: 'Katz'
}
}
});
return store.peekRecord('person', 'wat');
});

Ember.run(() => {
person.set('firstName', null);
person.set('lastName', null);
});

equal(person._internalModel.currentState.stateName, 'root.loaded.updated.uncommitted');
Ember.run(() => person.get('errors').add('firstName', 'is invalid') );

equal(person._internalModel.currentState.stateName, 'root.loaded.updated.invalid');

Ember.run(() => person.get('errors').add('lastName', 'is invalid') );

deepEqual(person.get('errors').toArray(), [
{ attribute: 'firstName', message: 'is invalid' },
{ attribute: 'lastName', message: 'is invalid' }
]);
});


test('adding errors root.loaded.created.invalid works', function() {
expect(3);

var person = run(() => {
return store.createRecord('person', {
id: 'wat',
firstName: 'Yehuda',
lastName: 'Katz'
});
});

Ember.run(() => {
person.set('firstName', null);
person.set('lastName', null);
});

equal(person._internalModel.currentState.stateName, 'root.loaded.created.uncommitted');

Ember.run(() => person.get('errors').add('firstName', 'is invalid') );

equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid');

Ember.run(() => person.get('errors').add('lastName', 'is invalid') );

deepEqual(person.get('errors').toArray(), [
{ attribute: 'firstName', message: 'is invalid' },
{ attribute: 'lastName', message: 'is invalid' }
]);
});

test('adding errors root.loaded.created.invalid works add + remove + add', function() {
expect(4);

var person = run(() => {
return store.createRecord('person', {
id: 'wat',
firstName: 'Yehuda'
});
});

Ember.run(() => {
person.set('firstName', null);
});

equal(person._internalModel.currentState.stateName, 'root.loaded.created.uncommitted');

Ember.run(() => person.get('errors').add('firstName', 'is invalid') );

equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid');

Ember.run(() => person.get('errors').remove('firstName'));

deepEqual(person.get('errors').toArray(), []);

Ember.run(() => person.get('errors').add('firstName', 'is invalid') );

deepEqual(person.get('errors').toArray(), [
{ attribute: 'firstName', message: 'is invalid' }
]);
});

test('adding errors root.loaded.created.invalid works add + (remove, add)', function() {
expect(4);

var person = run(() => {
return store.createRecord('person', {
id: 'wat',
firstName: 'Yehuda'
});
});

Ember.run(() => {
person.set('firstName', null);
});

equal(person._internalModel.currentState.stateName, 'root.loaded.created.uncommitted');

Ember.run(() => {
person.get('errors').add('firstName', 'is invalid');
});

equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid');

Ember.run(() => {
person.get('errors').remove('firstName');
person.get('errors').add('firstName', 'is invalid');
});


equal(person._internalModel.currentState.stateName, 'root.loaded.created.invalid');

deepEqual(person.get('errors').toArray(), [
{ attribute: 'firstName', message: 'is invalid' }
]);
});

0 comments on commit dd1ac98

Please sign in to comment.