Permalink
Browse files

Merge pull request #625 from toranb/master

The owner record is now an argument passed to findMany on the adapter
  • Loading branch information...
2 parents 4b35be2 + 61736c2 commit d443c9847d02b3143cd083af0f51672c9d483903 @wycats wycats committed Jan 15, 2013
View
2 packages/ember-data/lib/adapters/rest_adapter.js
@@ -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);
View
5 packages/ember-data/lib/system/record_arrays/many_array.js
@@ -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
View
10 packages/ember-data/lib/system/store.js
@@ -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);
},
/**
@@ -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"; }
},
@@ -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);
View
112 packages/ember-data/tests/unit/relationships_test.js
@@ -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");
+
+});
+

0 comments on commit d443c98

Please sign in to comment.