Skip to content

Commit

Permalink
Functional update for adapter populated record arrays
Browse files Browse the repository at this point in the history
permit for adapter populated record array contents to be updated by
calling `update` method.

Refs emberjs#3082
  • Loading branch information
vinilios committed May 19, 2015
1 parent f5f15df commit 8d7ca7d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/ember-data/lib/system/record-arrays/record-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export default Ember.ArrayProxy.extend(Ember.Evented, {

var store = get(this, 'store');
var type = get(this, 'type');
var query = get(this, 'query');

if (query) {
return store.findQuery(type, query, this);
}

return store.fetchAll(type, this);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,9 @@ Store = Service.extend({
@param {any} query an opaque query to be used by the adapter
@return {Promise} promise
*/
findQuery: function(typeName, query) {
findQuery: function(typeName, query, array) {
var type = this.modelFor(typeName);
var array = this.recordArrayManager
array = array || this.recordArrayManager
.createAdapterPopulatedRecordArray(type, query);

var adapter = this.adapterFor(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,61 @@ test("when a record is deleted in an adapter populated record array, it should b

equal(recordArray.get('length'), 2, "expected recordArray to contain exactly 2 records");
});

test("when an adapter populated record gets updated the array contents are also updated", function() {
expect(8);
var filteredPromise, filteredArr, findPromise, findArray;
var env = setupStore({ person: Person });
var store = env.store;
var array = [{ id: '1', name: "Scumbag Dale" }];

// resemble server side filtering
env.adapter.findQuery = function(store, type, query, recordArray) {
return Ember.RSVP.resolve(array.slice(query.slice));
};

// implement findAll to further test that query updates won't muddle
// with the non-query record arrays
env.adapter.findAll = function(store, type, sinceToken) {
return Ember.RSVP.resolve(array.slice(0));
};

run(function() {
filteredPromise = store.find(Person, { slice: 1 });
findPromise = store.find(Person);

// initialize adapter populated record array and assert initial state
filteredPromise.then(function(_filteredArr) {
filteredArr = _filteredArr;
equal(filteredArr.get('length'), 0, "No records for this query");
equal(filteredArr.get('isUpdating'), false, "Record array isUpdating state updated");
});

// initialize a record collection array and assert initial state
findPromise.then(function(_findArr) {
findArray = _findArr;
equal(findArray.get('length'), 1, "All records are included in collection array");
});
});

// a new element gets pushed in record array
run(function() {
array.push({ id: '2', name: "Scumbag Katz" });
filteredArr.update().then(function() {
equal(filteredArr.get('length'), 1, "The new record is returned and added in adapter populated array");
equal(filteredArr.get('isUpdating'), false, "Record array isUpdating state updated");
equal(findArray.get('length'), 2);
});
});

// element gets removed
run(function() {
array.pop(0);
filteredArr.update().then(function() {
equal(filteredArr.get('length'), 0, "Record removed from array");
// record not removed from the model collection
equal(findArray.get('length'), 2, "Record still remains in collection array");
});
});

});

0 comments on commit 8d7ca7d

Please sign in to comment.