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

Ember Data should delegate to the container to normalize the type key. #2766

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion packages/ember-data/lib/serializers/rest_serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ var RESTSerializer = JSONSerializer.extend({
@param {Object} options
*/
serializeIntoHash: function(hash, type, record, options) {
hash[type.typeKey] = this.serialize(record, options);
hash[this.typeForRoot(type.typeKey)] = this.serialize(record, options);
Copy link
Member Author

Choose a reason for hiding this comment

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

We should make a rootForType function to generate this root namespace based on the type key.

},

/**
Expand Down
9 changes: 5 additions & 4 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
InvalidError,
Adapter
} from "ember-data/system/adapter";
import { singularize } from "ember-inflector/system/string";
import {
Map
} from "ember-data/system/map";
Expand Down Expand Up @@ -77,8 +76,6 @@ var Promise = Ember.RSVP.Promise;
var copy = Ember.copy;
var Store;

var camelize = Ember.String.camelize;

// Implementors Note:
//
// The variables in this file are consistently named according to the following
Expand Down Expand Up @@ -1777,7 +1774,11 @@ Store = Ember.Object.extend({
@return {String} if the adapter can generate one, an ID
*/
_normalizeTypeKey: function(key) {
return camelize(singularize(key));
// Delegate to the container for normalization. The container
// requires a ':' to normalize so we add `model:` then slice off
// the first 6 characters to remove the `model:` in the return
// value
return this.container.normalize('model:' + key).slice(6);
Copy link
Member Author

Choose a reason for hiding this comment

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

@stefanpenner Is there a better way go get the normalized value for key?

Copy link
Member

Choose a reason for hiding this comment

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

not currently :(

}
});

Expand Down
4 changes: 2 additions & 2 deletions packages/ember-data/tests/unit/store/create_record_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test("creating a record by camel-case string finds the model", function() {
});

equal(record.get('foo'), attributes.foo, "The record is created");
equal(store.modelFor('someThing').typeKey, 'someThing');
equal(store.modelFor('someThing').typeKey, 'some-thing');
});

test("creating a record by dasherize string finds the model", function() {
Expand All @@ -54,7 +54,7 @@ test("creating a record by dasherize string finds the model", function() {
});

equal(record.get('foo'), attributes.foo, "The record is created");
equal(store.modelFor('some-thing').typeKey, 'someThing');
equal(store.modelFor('some-thing').typeKey, 'some-thing');
});

module("unit/store/createRecord - Store with models by camelCase", {
Expand Down
6 changes: 3 additions & 3 deletions packages/ember-data/tests/unit/store/model_for_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test("when fetching factory from string, sets a normalized key as typeKey", func
env.replaceContainerNormalize(camelize);

equal(container.normalize('some.post'), 'somePost', 'precond - container camelizes');
equal(store.modelFor("blog.post").typeKey, "blogPost", "typeKey is normalized to camelCase");
equal(store.modelFor("blog.post").typeKey, "blogPost", "typeKey is normalized by the container");
});

test("when fetching factory from string and dashing normalizer, sets a normalized key as typeKey", function() {
Expand All @@ -37,12 +37,12 @@ test("when fetching factory from string and dashing normalizer, sets a normalize
});

equal(container.normalize('some.post'), 'some-post', 'precond - container dasherizes');
equal(store.modelFor("blog.post").typeKey, "blogPost", "typeKey is normalized to camelCase");
equal(store.modelFor("blog.post").typeKey, "blog-post", "typeKey is normalized by the container");
});

test("when returning passed factory, sets a normalized key as typeKey", function() {
var factory = { typeKey: 'some-thing' };
equal(store.modelFor(factory).typeKey, "someThing", "typeKey is normalized to camelCase");
equal(store.modelFor(factory).typeKey, "some-thing", "typeKey is normalized to camelCase");
});

test("when returning passed factory without typeKey, allows it", function() {
Expand Down