Permalink
Browse files

Merge pull request #770 from Cyril-sf/bookkeeping

Preventing from dirtying internals while creating a record with an existing id
  • Loading branch information...
tomdale committed Mar 6, 2013
2 parents 185c80d + 278bfd9 commit 902e87f4747909b1b2859a376bc8f628cb8b1543
Showing with 21 additions and 0 deletions.
  1. +2 −0 packages/ember-data/lib/system/store.js
  2. +19 −0 packages/ember-data/tests/unit/store_test.js
@@ -1625,6 +1625,8 @@ DS.Store = Ember.Object.extend(DS._Mappable, {
clientIds = typeMap.clientIds,
cidToData = this.clientIdToData;
+ Ember.assert('The id ' + id + ' has already been used with another record of type ' + type.toString() + '.', !id || !idToClientIdMap[id]);
+
var clientId = ++this.clientIdCounter;
cidToData[clientId] = data;
@@ -414,6 +414,25 @@ test("a new record of a particular type is created via store.createRecord(type)"
equal(get(person, 'name'), "Braaahm Dale", "Even if no hash is supplied, `set` still worked");
});
+test("a new record with a specific id can't be created if this id is already used in the store", function() {
+ var store = DS.Store.create();
+ var Person = DS.Model.extend({
+ name: DS.attr('string'),
+ });
+ Person.reopenClass({
+ toString: function() {
+ return 'Person';
+ }
+ });
+ store.createRecord(Person, {id: 5});
+
+ raises(
+ function() { store.createRecord(Person, {id: 5}); },
+ /The id 5 has already been used with another record of type Person/,
+ "Creating a record with an if an id already in used in the store is disallowed"
+ );
+});
+
test("an initial data hash can be provided via store.createRecord(type, hash)", function() {
expect(6);
var store = DS.Store.create();

0 comments on commit 902e87f

Please sign in to comment.