Skip to content

Commit

Permalink
[BUGFIX beta] 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 #3082

(cherry picked from commit a24a806)

Conflicts:
	addon/-private/system/store.js
  • Loading branch information
vinilios authored and bmac committed Jan 11, 2016
1 parent 2227edd commit dad2970
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
9 changes: 7 additions & 2 deletions addon/-private/system/record-arrays/record-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ export default Ember.ArrayProxy.extend(Ember.Evented, {
update: function() {
if (get(this, 'isUpdating')) { return; }

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

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

return store.findAll(modelName, { reload: true });
},
Expand Down
4 changes: 2 additions & 2 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,10 @@ Store = Service.extend({
@param {any} query an opaque query to be used by the adapter
@return {Promise} promise
*/
query: function(modelName, query) {
query: function(modelName, query, array) {
assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
var typeClass = this.modelFor(modelName);
var array = this.recordArrayManager
array = array || this.recordArrayManager
.createAdapterPopulatedRecordArray(typeClass, query);

var adapter = this.adapterFor(modelName);
Expand Down
60 changes: 59 additions & 1 deletion tests/unit/adapter-populated-record-array-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createStore} from 'dummy/tests/helpers/store';
import {setupStore, createStore} from 'dummy/tests/helpers/store';
import Ember from 'ember';

import {module, test} from 'qunit';
Expand Down Expand Up @@ -73,3 +73,61 @@ test('recordArray.replace() throws error', function(assert) {
recordArray.replace();
}, Error("The result of a server query (on (subclass of DS.Model)) is immutable."), 'throws error');
});

test("when an adapter populated record gets updated the array contents are also updated", function(assert) {
assert.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.query = 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.query('person', { slice: 1 });
findPromise = store.findAll('person');

// initialize adapter populated record array and assert initial state
filteredPromise.then(function(_filteredArr) {
filteredArr = _filteredArr;
assert.equal(filteredArr.get('length'), 0, "No records for this query");
assert.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;
assert.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() {
assert.equal(filteredArr.get('length'), 1, "The new record is returned and added in adapter populated array");
assert.equal(filteredArr.get('isUpdating'), false, "Record array isUpdating state updated");
assert.equal(findArray.get('length'), 2);
});
});

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

});

0 comments on commit dad2970

Please sign in to comment.