Skip to content

Commit

Permalink
Merge pull request #3074 from emberjs/remove-passing-factories-to-sto…
Browse files Browse the repository at this point in the history
…re-methods

remove passing factories to store methods
  • Loading branch information
fivetanley committed Jun 3, 2015
2 parents 7e275f4 + d7fb2c3 commit 3d0aeb3
Show file tree
Hide file tree
Showing 50 changed files with 883 additions and 691 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ var ActiveModelSerializer = RESTSerializer.extend({
relationship keys.
@method keyForRelationship
@param {String} relationshipTypeKey
@param {String} relationshipModelName
@param {String} kind
@return String
*/
keyForRelationship: function(relationshipTypeKey, kind) {
var key = decamelize(relationshipTypeKey);
keyForRelationship: function(relationshipModelName, kind) {
var key = decamelize(relationshipModelName);
if (kind === "belongsTo") {
return key + "_id";
} else if (kind === "hasMany") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@ module("integration/active_model - AMS-namespaced-model-names", {
SuperVillain = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
evilMinions: DS.hasMany("evilMinion")
evilMinions: DS.hasMany('evil-minion')
});

EvilMinion = DS.Model.extend({
superVillain: DS.belongsTo('superVillain'),
superVillain: DS.belongsTo('super-villain'),
name: DS.attr('string')
});
YellowMinion = EvilMinion.extend();
DoomsdayDevice = DS.Model.extend({
name: DS.attr('string'),
evilMinion: DS.belongsTo('evilMinion', { polymorphic: true })
evilMinion: DS.belongsTo('evil-minion', { polymorphic: true })
});
MediocreVillain = DS.Model.extend({
name: DS.attr('string'),
evilMinions: DS.hasMany('evilMinion', { polymorphic: true })
evilMinions: DS.hasMany('evil-minion', { polymorphic: true })
});
env = setupStore({
superVillain: SuperVillain,
evilMinion: EvilMinion,
'evilMinions/yellowMinion': YellowMinion,
doomsdayDevice: DoomsdayDevice,
mediocreVillain: MediocreVillain
mediocreVillain: MediocreVillain,
yellowMinion: YellowMinion
});
env.store.modelFor('superVillain');
env.store.modelFor('evilMinion');
env.store.modelFor('evilMinions/yellowMinion');
env.store.modelFor('doomsdayDevice');
env.store.modelFor('mediocreVillain');
env.store.modelFor('super-villain');
env.store.modelFor('evil-minion');
env.store.modelFor('evil-minions/yellow-minion');
env.store.modelFor('doomsday-device');
env.store.modelFor('mediocre-villain');
env.registry.register('serializer:application', DS.ActiveModelSerializer);
env.registry.register('serializer:-active-model', DS.ActiveModelSerializer);
env.registry.register('adapter:-active-model', DS.ActiveModelAdapter);
Expand All @@ -49,8 +50,8 @@ module("integration/active_model - AMS-namespaced-model-names", {
test("serialize polymorphic", function() {
var tom, ray;
run(function() {
tom = env.store.createRecord('evilMinions/yellowMinion', { name: "Alex", id: "124" });
ray = env.store.createRecord(DoomsdayDevice, { evilMinion: tom, name: "DeathRay" });
tom = env.store.createRecord('evil-minions/yellow-minion', { name: "Alex", id: "124" });
ray = env.store.createRecord('doomsday-device', { evilMinion: tom, name: "DeathRay" });
});

var json = env.amsSerializer.serialize(ray._createSnapshot());
Expand All @@ -66,8 +67,8 @@ test("serialize polymorphic when type key is not camelized", function() {
YellowMinion.modelName = 'evil-minions/yellow-minion';
var tom, ray;
run(function() {
tom = env.store.createRecord(YellowMinion, { name: "Alex", id: "124" });
ray = env.store.createRecord(DoomsdayDevice, { evilMinion: tom, name: "DeathRay" });
tom = env.store.createRecord('yellow-minion', { name: "Alex", id: "124" });
ray = env.store.createRecord('doomsday-device', { evilMinion: tom, name: "DeathRay" });
});

var json = env.amsSerializer.serialize(ray._createSnapshot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ module("integration/active_model - ActiveModelSerializer", {
SuperVillain = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
homePlanet: DS.belongsTo("homePlanet"),
evilMinions: DS.hasMany("evilMinion")
homePlanet: DS.belongsTo('home-planet'),
evilMinions: DS.hasMany('evil-minion')
});
HomePlanet = DS.Model.extend({
name: DS.attr('string'),
superVillains: DS.hasMany('superVillain', { async: true })
superVillains: DS.hasMany('super-villain', { async: true })
});
EvilMinion = DS.Model.extend({
superVillain: DS.belongsTo('superVillain'),
superVillain: DS.belongsTo('super-villain'),
name: DS.attr('string')
});
YellowMinion = EvilMinion.extend();
DoomsdayDevice = DS.Model.extend({
name: DS.attr('string'),
evilMinion: DS.belongsTo('evilMinion', { polymorphic: true })
evilMinion: DS.belongsTo('evil-minion', { polymorphic: true })
});
MediocreVillain = DS.Model.extend({
name: DS.attr('string'),
evilMinions: DS.hasMany('evilMinion', { polymorphic: true })
evilMinions: DS.hasMany('evil-minion', { polymorphic: true })
});
env = setupStore({
superVillain: SuperVillain,
Expand All @@ -35,12 +35,12 @@ module("integration/active_model - ActiveModelSerializer", {
doomsdayDevice: DoomsdayDevice,
mediocreVillain: MediocreVillain
});
env.store.modelFor('superVillain');
env.store.modelFor('homePlanet');
env.store.modelFor('evilMinion');
env.store.modelFor('yellowMinion');
env.store.modelFor('doomsdayDevice');
env.store.modelFor('mediocreVillain');
env.store.modelFor('super-villain');
env.store.modelFor('home-planet');
env.store.modelFor('evil-minion');
env.store.modelFor('yellow-minion');
env.store.modelFor('doomsday-device');
env.store.modelFor('mediocre-villain');
env.registry.register('serializer:application', DS.ActiveModelSerializer);
env.registry.register('serializer:-active-model', DS.ActiveModelSerializer);
env.registry.register('adapter:-active-model', DS.ActiveModelAdapter);
Expand All @@ -56,8 +56,8 @@ module("integration/active_model - ActiveModelSerializer", {
test("serialize", function() {
var tom;
run(function() {
league = env.store.createRecord(HomePlanet, { name: "Villain League", id: "123" });
tom = env.store.createRecord(SuperVillain, { firstName: "Tom", lastName: "Dale", homePlanet: league });
league = env.store.createRecord('home-planet', { name: "Villain League", id: "123" });
tom = env.store.createRecord('super-villain', { firstName: "Tom", lastName: "Dale", homePlanet: league });
});

var json = env.amsSerializer.serialize(tom._createSnapshot());
Expand All @@ -71,7 +71,7 @@ test("serialize", function() {

test("serializeIntoHash", function() {
run(function() {
league = env.store.createRecord(HomePlanet, { name: "Umber", id: "123" });
league = env.store.createRecord('home-planet', { name: "Umber", id: "123" });
});
var json = {};

Expand All @@ -87,7 +87,7 @@ test("serializeIntoHash", function() {
test("serializeIntoHash with decamelized types", function() {
HomePlanet.modelName = 'home-planet';
run(function() {
league = env.store.createRecord(HomePlanet, { name: "Umber", id: "123" });
league = env.store.createRecord('home-planet', { name: "Umber", id: "123" });
});
var json = {};

Expand All @@ -103,7 +103,7 @@ test("serializeIntoHash with decamelized types", function() {

test("normalize", function() {
SuperVillain.reopen({
yellowMinion: DS.belongsTo('yellowMinion')
yellowMinion: DS.belongsTo('yellow-minion')
});

var superVillain_hash = { first_name: "Tom", last_name: "Dale", home_planet_id: "123", evil_minion_ids: [1,2] };
Expand Down Expand Up @@ -156,7 +156,7 @@ test("extractSingle", function() {
});

run(function() {
env.store.find("superVillain", 1).then(function(minion) {
env.store.find('super-villain', 1).then(function(minion) {
equal(minion.get('firstName'), "Tom");
});
});
Expand All @@ -182,7 +182,7 @@ test("extractArray", function() {
}]);

run(function() {
env.store.find("superVillain", 1).then(function(minion) {
env.store.find('super-villain', 1).then(function(minion) {
equal(minion.get('firstName'), "Tom");
});
});
Expand All @@ -191,8 +191,8 @@ test("extractArray", function() {
test("serialize polymorphic", function() {
var tom, ray;
run(function() {
tom = env.store.createRecord(YellowMinion, { name: "Alex", id: "124" });
ray = env.store.createRecord(DoomsdayDevice, { evilMinion: tom, name: "DeathRay" });
tom = env.store.createRecord('yellow-minion', { name: "Alex", id: "124" });
ray = env.store.createRecord('doomsday-device', { evilMinion: tom, name: "DeathRay" });
});

var json = env.amsSerializer.serialize(ray._createSnapshot());
Expand All @@ -208,8 +208,8 @@ test("serialize polymorphic when type key is not camelized", function() {
YellowMinion.modelName = 'yellow-minion';
var tom, ray;
run(function() {
tom = env.store.createRecord(YellowMinion, { name: "Alex", id: "124" });
ray = env.store.createRecord(DoomsdayDevice, { evilMinion: tom, name: "DeathRay" });
tom = env.store.createRecord('yellow-minion', { name: "Alex", id: "124" });
ray = env.store.createRecord('doomsday-device', { evilMinion: tom, name: "DeathRay" });
});

var json = env.amsSerializer.serialize(ray._createSnapshot());
Expand All @@ -220,7 +220,7 @@ test("serialize polymorphic when type key is not camelized", function() {
test("serialize polymorphic when associated object is null", function() {
var ray, json;
run(function() {
ray = env.store.createRecord(DoomsdayDevice, { name: "DeathRay" });
ray = env.store.createRecord('doomsday-device', { name: "DeathRay" });
json = env.amsSerializer.serialize(ray._createSnapshot());
});

Expand Down
13 changes: 7 additions & 6 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
var parentRecord = snapshot.type.inverseFor(relationship.key);
if (parentRecord) {
var name = parentRecord.name;
var embeddedSerializer = this.store.serializerFor(embeddedSnapshot.type);
var embeddedSerializer = this.store.serializerFor(embeddedSnapshot.modelName);
var parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind, 'deserialize');
if (parentKey) {
delete json[parentKey];
Expand Down Expand Up @@ -394,7 +394,7 @@ function extractEmbeddedRecords(serializer, store, typeClass, partial) {

typeClass.eachRelationship(function(key, relationship) {
if (serializer.hasDeserializeRecordsOption(key)) {
var embeddedTypeClass = store.modelFor(relationship.type.modelName);
var embeddedTypeClass = store.modelFor(relationship.type);
if (relationship.kind === "hasMany") {
if (relationship.options.polymorphic) {
extractEmbeddedHasManyPolymorphic(store, key, partial);
Expand Down Expand Up @@ -426,7 +426,7 @@ function extractEmbeddedHasMany(store, key, embeddedTypeClass, hash) {
var embeddedSerializer = store.serializerFor(embeddedTypeClass.modelName);
forEach(hash[key], function(data) {
var embeddedRecord = embeddedSerializer.normalize(embeddedTypeClass, data, null);
store.push(embeddedTypeClass, embeddedRecord);
store.push(embeddedTypeClass.modelName, embeddedRecord);
ids.push(embeddedRecord.id);
});

Expand All @@ -445,9 +445,10 @@ function extractEmbeddedHasManyPolymorphic(store, key, hash) {
var modelName = data.type;
var embeddedSerializer = store.serializerFor(modelName);
var embeddedTypeClass = store.modelFor(modelName);
// var primaryKey = embeddedSerializer.get('primaryKey');

var embeddedRecord = embeddedSerializer.normalize(embeddedTypeClass, data, null);
store.push(embeddedTypeClass, embeddedRecord);
store.push(embeddedTypeClass.modelName, embeddedRecord);
ids.push({ id: embeddedRecord.id, type: modelName });
});

Expand All @@ -462,7 +463,7 @@ function extractEmbeddedBelongsTo(store, key, embeddedTypeClass, hash) {

var embeddedSerializer = store.serializerFor(embeddedTypeClass.modelName);
var embeddedRecord = embeddedSerializer.normalize(embeddedTypeClass, hash[key], null);
store.push(embeddedTypeClass, embeddedRecord);
store.push(embeddedTypeClass.modelName, embeddedRecord);

hash[key] = embeddedRecord.id;
//TODO Need to add a reference to the parent later so relationship works between both `belongsTo` records
Expand All @@ -480,7 +481,7 @@ function extractEmbeddedBelongsToPolymorphic(store, key, hash) {
var embeddedTypeClass = store.modelFor(modelName);

var embeddedRecord = embeddedSerializer.normalize(embeddedTypeClass, data, null);
store.push(embeddedTypeClass, embeddedRecord);
store.push(embeddedTypeClass.modelName, embeddedRecord);

hash[key] = embeddedRecord.id;
hash[key + 'Type'] = modelName;
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ export default Serializer.extend({
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
}

var relationshipType = snapshot.type.determineRelationshipType(relationship);
var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store);

if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany') {
json[payloadKey] = snapshot.hasMany(key, { ids: true });
Expand Down Expand Up @@ -729,7 +729,7 @@ export default Serializer.extend({
@return {Object} json The deserialized payload
*/
extract: function(store, typeClass, payload, id, requestType) {
this.extractMeta(store, typeClass, payload);
this.extractMeta(store, typeClass.modelName, payload);

var specificExtract = "extract" + requestType.charAt(0).toUpperCase() + requestType.substr(1);
return this[specificExtract](store, typeClass, payload, id, requestType);
Expand Down
20 changes: 10 additions & 10 deletions packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ var RESTSerializer = JSONSerializer.extend({
var primaryRecord;

for (var prop in payload) {
var typeName = this.modelNameFromPayloadKey(prop);
var modelName = this.modelNameFromPayloadKey(prop);

if (!store.modelFactoryFor(typeName)) {
Ember.warn(this.warnMessageNoModelForKey(prop, typeName), false);
if (!store.modelFactoryFor(modelName)) {
Ember.warn(this.warnMessageNoModelForKey(prop, modelName), false);
continue;
}
var isPrimary = this.isPrimaryType(store, typeName, primaryTypeClass);
var isPrimary = this.isPrimaryType(store, modelName, primaryTypeClass);
var value = payload[prop];

if (value === null) {
Expand All @@ -282,7 +282,7 @@ var RESTSerializer = JSONSerializer.extend({
continue;
}

var normalizedArray = this.normalizeArray(store, typeName, value, prop);
var normalizedArray = this.normalizeArray(store, modelName, value, prop);

/*jshint loopfunc:true*/
forEach.call(normalizedArray, function(hash) {
Expand All @@ -298,7 +298,7 @@ var RESTSerializer = JSONSerializer.extend({
if (isFirstCreatedRecord || isUpdatedRecord) {
primaryRecord = hash;
} else {
store.push(typeName, hash);
store.push(modelName, hash);
}
}, this);
}
Expand Down Expand Up @@ -440,7 +440,7 @@ var RESTSerializer = JSONSerializer.extend({

normalizeArray: function(store, typeName, arrayHash, prop) {
var typeClass = store.modelFor(typeName);
var typeSerializer = store.serializerFor(typeClass);
var typeSerializer = store.serializerFor(typeName);

/*jshint loopfunc:true*/
return map.call(arrayHash, function(hash) {
Expand Down Expand Up @@ -493,12 +493,12 @@ var RESTSerializer = JSONSerializer.extend({
Ember.warn(this.warnMessageNoModelForKey(prop, modelName), false);
continue;
}
var type = store.modelFor(modelName);
var typeSerializer = store.serializerFor(type);
var typeClass = store.modelFor(modelName);
var typeSerializer = store.serializerFor(modelName);

/*jshint loopfunc:true*/
var normalizedArray = map.call(Ember.makeArray(payload[prop]), function(hash) {
return typeSerializer.normalize(type, hash, prop);
return typeSerializer.normalize(typeClass, hash, prop);
}, this);

store.pushMany(modelName, normalizedArray);
Expand Down
6 changes: 5 additions & 1 deletion packages/ember-data/lib/system/debug/debug-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ export default Ember.DataAdapter.extend({
return columns;
},

getRecords: function(modelName) {
getRecords: function(modelNameOrFactory) {
// TODO: Ask Teddy what we should do here.
// Ideally this should always get passed a string.

var modelName = typeof modelNameOrFactory === 'string' ? modelNameOrFactory : modelNameOrFactory.modelName;
return this.get('store').all(modelName);
},

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {

Ember.assert("You cannot add '" + type.modelName + "' records to this polymorphic relationship.", !get(this, 'isPolymorphic'));

record = store.createRecord(type, hash);
record = store.createRecord(type.modelName, hash);
this.pushObject(record);

return record;
Expand Down
1 change: 0 additions & 1 deletion packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var retrieveFromCurrentState = Ember.computed('currentState', function(key) {
return get(this._internalModel.currentState, key);
}).readOnly();


/**
The model class that all Ember Data records descend from.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export default RecordArray.extend({
load: function(data) {
var store = get(this, 'store');
var type = get(this, 'type');
var records = store.pushMany(type, data);
var meta = store.metadataFor(type);
var modelName = type.modelName;
var records = store.pushMany(modelName, data);
var meta = store.metadataFor(modelName);

//TODO Optimize
var internalModels = Ember.A(records).mapBy('_internalModel');
Expand Down
Loading

0 comments on commit 3d0aeb3

Please sign in to comment.