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

The owner record is now an argument passed to findMany on the adapter #625

Merged
merged 1 commit into from Jan 15, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ember-data/lib/adapters/rest_adapter.js
Expand Up @@ -265,7 +265,7 @@ DS.RESTAdapter = DS.Adapter.extend({
});
},

findMany: function(store, type, ids) {
findMany: function(store, type, ids, owner) {
var root = this.rootForType(type);
ids = this.serializeIds(ids);

Expand Down
5 changes: 3 additions & 2 deletions packages/ember-data/lib/system/record_arrays/many_array.js
Expand Up @@ -66,9 +66,10 @@ DS.ManyArray = DS.RecordArray.extend({
fetch: function() {
var references = get(this, 'content'),
store = get(this, 'store'),
type = get(this, 'type');
type = get(this, 'type'),
owner = get(this, 'owner');

store.fetchUnloadedReferences(type, references);
store.fetchUnloadedReferences(type, references, owner);
},

// Overrides Ember.Array's replace method to implement
Expand Down
10 changes: 5 additions & 5 deletions packages/ember-data/lib/system/store.js
Expand Up @@ -590,9 +590,9 @@ DS.Store = Ember.Object.extend(DS._Mappable, {
then converts the needed `clientId`s to IDs and invokes `findMany`
on the adapter.
*/
fetchUnloadedReferences: function(type, references) {
fetchUnloadedReferences: function(type, references, owner) {
var neededReferences = this.neededReferences(type, references);
this.fetchMany(type, neededReferences);
this.fetchMany(type, neededReferences, owner);
},

/**
Expand All @@ -606,15 +606,15 @@ DS.Store = Ember.Object.extend(DS._Mappable, {
method) or when the data underlying an existing relationship
changes (via the `fetchUnloadedReferences` method).
*/
fetchMany: function(type, references) {
fetchMany: function(type, references, owner) {
if (!references.length) { return; }

var ids = map(references, function(reference) {
return reference.id;
});

var adapter = this.adapterForType(type);
if (adapter && adapter.findMany) { adapter.findMany(this, type, ids); }
if (adapter && adapter.findMany) { adapter.findMany(this, type, ids, owner); }
else { throw "Adapter is either null or does not implement `findMany` method"; }
},

Expand Down Expand Up @@ -715,7 +715,7 @@ DS.Store = Ember.Object.extend(DS._Mappable, {
}
}

this.fetchMany(type, neededReferences);
this.fetchMany(type, neededReferences, record);
} else {
// all requested records are available
manyArray.set('isLoaded', true);
Expand Down
112 changes: 112 additions & 0 deletions packages/ember-data/tests/unit/relationships_test.js
Expand Up @@ -502,3 +502,115 @@ test("calling createRecord and passing in an undefined value for a relationship
strictEqual(person.get('tag'), null, "undefined values should return null relationships");
});

test("findMany is passed the owner record for adapters when some of the object graph is already loaded", function() {
var Occupation = DS.Model.extend({
description: DS.attr('string')
});

Occupation.toString = function() { return "Occupation"; };

var Person = DS.Model.extend({
name: DS.attr('string'),
occupations: DS.hasMany(Occupation)
});

Person.toString = function() { return "Person"; };

Occupation.reopen({
person: DS.belongsTo(Person)
});

var store = DS.Store.create({
adapter: DS.Adapter.create({
findMany: function(store, type, ids, owner) {
equal(type, Occupation, "type should be Occupation");
deepEqual(ids, ['5', '2'], "ids should be 5 and 2");
equal(get(owner, 'id'), 1, "the owner record id should be 1");

stop();

setTimeout(function() {
start();
store.loadMany(type, ids, [{ id: 5, description: "fifth" }, { id: 2, description: "second" }]);

equal(get(person, 'name'), "Tom Dale", "the person is still Tom Dale");
equal(get(person, 'occupations.length'), 2, "the occupation objects still exist");
equal(get(get(person, 'occupations').objectAt(0), 'description'), "fifth", "the occupation is the fifth");
equal(get(get(person, 'occupations').objectAt(0), 'isLoaded'), true, "the occupation is now loaded");
}, 1);
}
})
});

store.load(Person, 1, { id: 1, name: "Tom Dale", occupations: [5, 2] });

var person = store.find(Person, 1);

equal(get(person, 'isLoaded'), true, "isLoaded should be true");
equal(get(person, 'occupations.length'), 2, "the list of occupations should have the correct length");

});

test("findMany is passed the owner record for adapters when none of the object graph is loaded", function() {
var Occupation = DS.Model.extend({
description: DS.attr('string')
});

Occupation.toString = function() { return "Occupation"; };

var Person = DS.Model.extend({
name: DS.attr('string'),
occupations: DS.hasMany(Occupation)
});

Person.toString = function() { return "Person"; };

Occupation.reopen({
person: DS.belongsTo(Person)
});

var store = DS.Store.create({
adapter: DS.Adapter.create({
findMany: function(store, type, ids, owner) {
equal(type, Occupation, "type should be Occupation");
deepEqual(ids, ['5', '2'], "ids should be 5 and 2");
equal(get(owner, 'id'), 1, "the owner record id should be 1");

stop();

setTimeout(function() {
start();
store.loadMany(type, ids, [{ id: 5, description: "fifth" }, { id: 2, description: "second" }]);

equal(get(person, 'name'), "Tom Dale", "the person is still Tom Dale");
equal(get(person, 'occupations.length'), 2, "the occupation objects still exist");
equal(get(get(person, 'occupations').objectAt(0), 'description'), "fifth", "the occupation is the fifth");
equal(get(get(person, 'occupations').objectAt(0), 'isLoaded'), true, "the occupation is now loaded");
}, 1);
},

find: function(store, type, id) {
equal(type, Person, "type should be Person");
equal(id, 1, "id should be 1");

stop();

setTimeout(function() {
start();
store.load(type, id, { id: 1, name: "Tom Dale", occupations: [5, 2] });

equal(get(person, 'name'), "Tom Dale", "The person is now populated");
equal(get(person, 'occupations.length'), 2, "the occupations Array already exists");
equal(get(get(person, 'occupations').objectAt(0), 'isLoaded'), false, "the occupation objects exist, but are not yet loaded");
}, 1);
}
})
});

var person = store.find(Person, 1);

equal(get(person, 'isLoaded'), false, "isLoaded should be false");
equal(get(person, 'occupations.length'), 0, "occupations should be empty");

});