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

[FEATURE ds-pushpayload-return] Change pushPayload to return a value. #4110

Merged
merged 1 commit into from
Jan 27, 2016
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
5 changes: 5 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ entry in `config/features.json`.

Pass options specified for a `DS.attr` to the `DS.Tranform`'s `serialize` and
`deserialize` methods (described in [RFC 1](https://github.com/emberjs/rfcs/pull/1))

- `ds-pushpayload-return`

Enables `pushPayload` to return the model(s) that are created or
updated via the internal `store.push`. [PR 4110](https://github.com/emberjs/data/pull/4110)
6 changes: 5 additions & 1 deletion addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,11 @@ Store = Service.extend({
assert(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${Ember.inspect(modelName)}`, typeof modelName === 'string');
serializer = this.serializerFor(modelName);
}
this._adapterRun(() => serializer.pushPayload(this, payload));
if (isEnabled('ds-pushpayload-return')) {
return this._adapterRun(() => { return serializer.pushPayload(this, payload); });
} else {
this._adapterRun(() => serializer.pushPayload(this, payload));
}
},

/**
Expand Down
7 changes: 6 additions & 1 deletion addon/serializers/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assert, runInDebug, warn } from 'ember-data/-private/debug';
import JSONSerializer from 'ember-data/serializers/json';
import normalizeModelName from 'ember-data/-private/system/normalize-model-name';
import { pluralize, singularize } from 'ember-inflector';
import isEnabled from 'ember-data/-private/features';

var dasherize = Ember.String.dasherize;

Expand Down Expand Up @@ -178,7 +179,11 @@ const JSONAPISerializer = JSONSerializer.extend({
*/
pushPayload(store, payload) {
let normalizedPayload = this._normalizeDocumentHelper(payload);
store.push(normalizedPayload);
if (isEnabled('ds-pushpayload-return')) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we do this in the store instead, and lets have the serializer return normalized json payload

Copy link
Author

@workmanw workmanw May 18, 2016

Choose a reason for hiding this comment

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

@igorT I'm not 100% sure I follow.

Currently store.pushPayload just looks up the associated serializer and invokes serializer.pushPayload. The serializers uniquely handle their normalize operations and then callback to the store (store.push).

So with all that said, I read your comment as saying, "Can we move this feature check into the store only?" In order to do that, I think you would 1) have to always just return the store.push result, or 2) create a new API mechanism for the store to ask the serializer to normalize the payload so the pushPayload` action didn't have to pass through the serializer (which I think is what maybe you were hinting at).

I'm happy to make additional changes or spend additional time continue to improve this feature. Please also tell me if I misunderstood your request. Let me know what you'd like me to do.

return store.push(normalizedPayload);
} else {
store.push(normalizedPayload);
}
},

/**
Expand Down
7 changes: 6 additions & 1 deletion addon/serializers/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import normalizeModelName from "ember-data/-private/system/normalize-model-name"
import {singularize} from "ember-inflector";
import coerceId from "ember-data/-private/system/coerce-id";
import { modelHasAttributeOrRelationshipNamedType } from "ember-data/-private/utils";
import isEnabled from 'ember-data/-private/features';

var camelize = Ember.String.camelize;

Expand Down Expand Up @@ -403,7 +404,11 @@ var RESTSerializer = JSONSerializer.extend({
});
}

store.push(documentHash);
if (isEnabled('ds-pushpayload-return')) {
return store.push(documentHash);
} else {
store.push(documentHash);
}
},

/**
Expand Down
3 changes: 2 additions & 1 deletion config/features.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"ds-finder-include": null,
"ds-references": null,
"ds-transform-pass-options": null
"ds-transform-pass-options": null,
"ds-pushpayload-return": null
}
31 changes: 31 additions & 0 deletions tests/unit/store/push-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {module, test} from 'qunit';

import DS from 'ember-data';

import isEnabled from 'ember-data/-private/features';

var env, store, Person, PhoneNumber, Post;
var attr = DS.attr;
var hasMany = DS.hasMany;
Expand Down Expand Up @@ -668,6 +670,35 @@ test("Calling push with unknown keys should not warn by default", function(asser
}, /The payload for 'person' contains these unknown keys: \[emailAddress,isMascot\]. Make sure they've been defined in your model./);
});

if (isEnabled('ds-pushpayload-return')) {
test("Calling pushPayload returns records", function(assert) {
env.registry.register('serializer:person', DS.RESTSerializer);

var people;

run(function() {
people = store.pushPayload('person', {
people: [{
id: '1',
firstName: "Robert",
lastName: "Jackson"
}, {
id: '2',
firstName: "Matthew",
lastName: "Beale"
}]
});
});

assert.equal(people.length, 2, "both records were returned by `store.pushPayload`");

assert.equal(people[0].get('firstName'), "Robert", "pushPayload returns pushed records");
assert.equal(people[0].get('lastName'), "Jackson", "pushPayload returns pushed records");
assert.equal(people[1].get('firstName'), "Matthew", "pushPayload returns pushed records");
assert.equal(people[1].get('lastName'), "Beale", "pushPayload returns pushed records");
});
}

module("unit/store/push - DS.Store#push with JSON-API", {
beforeEach() {
var Person = DS.Model.extend({
Expand Down