Skip to content

Loading…

Support for deleting newly added records to many arrays #252

Closed
wants to merge 3 commits into from

4 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
10 packages/ember-data/lib/system/record_array/many_array.js
@@ -33,6 +33,7 @@ DS.ManyArray = DS.RecordArray.extend({
var parentRecord = get(this, 'parentRecord');
var pendingParent = parentRecord && !get(parentRecord, 'id');
var stateManager = get(this, 'stateManager');
+ var store = get(this, 'store');
added = added.map(function(record) {
Ember.assert("You can only add records of " + (get(this, 'type') && get(this, 'type').toString()) + " to this association.", !get(this, 'type') || (get(this, 'type') === record.constructor));
@@ -44,12 +45,13 @@ DS.ManyArray = DS.RecordArray.extend({
this.assignInverse(record, parentRecord);
stateManager.send('recordWasAdded', record);
-
- return record.get('clientId');
+
+ var clientId = record.get('clientId');
+ store.registerRecordArrayForClientId(this, clientId);
+
+ return clientId;
}, this);
- var store = this.store;
-
var len = index+removed, record;
for (var i = index; i < len; i++) {
// TODO: null out inverse FK
View
8 packages/ember-data/lib/system/store.js
@@ -605,12 +605,16 @@ DS.Store = Ember.Object.extend({
var array = DS.ManyArray.create({ type: type, content: clientIds, store: this });
clientIds.forEach(function(clientId) {
- var recordArrays = this.recordArraysForClientId(clientId);
- recordArrays.add(array);
+ this.registerRecordArrayForClientId(array, clientId);
}, this);
return array;
},
+
+ registerRecordArrayForClientId: function(array, clientId) {
+ var recordArrays = this.recordArraysForClientId(clientId);
+ recordArrays.add(array);
+ },
updateRecordArrayFilter: function(array, type, filter) {
var typeMap = this.typeMapFor(type),
View
12 packages/ember-data/tests/unit/record_array_test.js
@@ -72,6 +72,18 @@ test("a newly created record is removed from a record array when it is deleted",
});
});
+test("a newly created record is removed from a collection when it is deleted", function() {
+ var store = DS.Store.create({ adapter: null }),
+ Group = DS.Model.extend({ people: DS.hasMany('Person') }),
+ group;
+
+ group = store.createRecord(Group);
+ group.get('people').pushObject(store.createRecord(Person));
+ equal(group.get('people').get('length'), 1, "length is 1 after adding record");
+ group.get('people').objectAt(0).deleteRecord();
+ equal(group.get('people').get('length'), 0, "length is 0 after deleting record");
+});
+
test("a record array can have a filter on it", function() {
var store = DS.Store.create();
Something went wrong with that request. Please try again.