Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] Functional update for adapter populated record arrays #4050

Merged
merged 1 commit into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
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 @@ -955,10 +955,10 @@ Store = Service.extend({
@param {any} query an opaque query to be used by the adapter
@return {Promise} promise
*/
query(modelName, query) {
query(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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe missing array here: adapter populated record **array** gets...

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");
});
});

});