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

Add support for the include option to source.find and source.findLinked #66

Merged
merged 2 commits into from
Aug 15, 2015
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
12 changes: 7 additions & 5 deletions lib/ember-orbit/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ var Store = Source.extend({
}, null, "OE: Store#filter of " + type));
},

find: function(type, id) {
find: function(type, id, options) {
var _this = this;
this._verifyType(type);

var promise = this.orbitSource.find(type, id).then(function(data) {
var promise = this.orbitSource.find(type, id, options).then(function(data) {
return _this._lookupFromData(type, data);
});

Expand Down Expand Up @@ -192,15 +192,15 @@ var Store = Source.extend({
return this._request(promise);
},

findLinked: function(type, id, field) {
findLinked: function(type, id, field, options) {
var _this = this;
this._verifyType(type);
id = this._normalizeId(id);

var linkType = get(this, 'schema').linkProperties(type, field).model;
this._verifyType(linkType);

var promise = this.orbitSource.findLinked(type, id, field).then(function(data) {
var promise = this.orbitSource.findLinked(type, id, field, options).then(function(data) {
return _this._lookupFromData(linkType, data);
});

Expand Down Expand Up @@ -265,7 +265,9 @@ var Store = Source.extend({
var linkType = get(this, 'schema').linkProperties(type, field).model;
this._verifyType(linkType);

var relatedIds = Object.keys(this.orbitSource.retrieve([type, id, '__rel', field]));
var links = this.orbitSource.retrieve([type, id, '__rel', field]);
if(links === undefined) throw new Error("Link " + [type,id,field].join("/") + " is not loaded. Add it to your includes e.g. find('" + type + "', '" + id + "', {include: ['" + field + "']})");
var relatedIds = Object.keys(links);

if (linkType && Ember.isArray(relatedIds) && relatedIds.length > 0) {
return this.retrieve(linkType, relatedIds);
Expand Down
31 changes: 31 additions & 0 deletions test/tests/unit/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@ test("#find will asynchronously return an array of records when called with a `t
});
});

test("#find passes options through to find on the orbit source", function(){
expect(1);

var options = {include: 'moons'};
store.orbitSource.find = sinon.stub().returns(Ember.RSVP.resolve({}));

store.find('planet', 'planet1', options);
ok(store.orbitSource.find.calledWith('planet', 'planet1', options), "options were passed through");
});

test("#findLinked passes options through to findLinked on the orbit source", function(){
expect(1);

var options = {include: 'moons'};
store.orbitSource.findLinked = sinon.stub().returns(Ember.RSVP.resolve({}));

store.findLinked('planet', 'planet1', 'moons', options);
ok(store.orbitSource.findLinked.calledWith('planet', 'planet1', 'moons', options), "options were passed through");
});

test("#remove will asynchronously remove a record when called with a `type` and a single `id`", function() {
expect(2);

Expand Down Expand Up @@ -293,6 +313,17 @@ test("#retrieve can synchronously retrieve all records of a particular type", fu
});
});

test("#retrieveLinks throws an error if the link hasn't been loaded yet", function(){
store.orbitSource.retrieve = sinon.stub().withArgs(['planet', 'planet1', '__rel', 'moons']).returns(undefined);

try {
store.retrieveLinks('planet', 'planet1', 'moons');
}
catch(error){
equal(error.message, "Link planet/planet1/moons is not loaded. Add it to your includes e.g. find('planet', 'planet1', {include: ['moons']})");
}
});

test("#all returns a live RecordArray that stays in sync with records of one type", function() {
expect(4);

Expand Down