Skip to content

Commit

Permalink
supporting sideloads
Browse files Browse the repository at this point in the history
  • Loading branch information
shajith committed Jul 6, 2012
1 parent fb70156 commit e50cf41
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
24 changes: 18 additions & 6 deletions dist/ember-resource.js
Expand Up @@ -1046,6 +1046,7 @@ if (typeof this === 'object') this.LRUCache = LRUCache;
},

fetch: function() {
var ajaxOptions, sideloads;
if (!Ember.get(this, 'isFetchable')) return $.when();

var url = this.resourceURL();
Expand All @@ -1059,13 +1060,20 @@ if (typeof this === 'object') this.LRUCache = LRUCache;
self.willFetch.call(self);
Ember.sendEvent(self, 'willFetch');

this.deferedFetch = Ember.Resource.ajax({
ajaxOptions = {
url: url,
resource: this,
operation: 'read',
success: function(json) {
self.updateWithApiData(json);
}
operation: 'read'
};

sideloads = this.constructor.sideloads;

if(sideloads && sideloads.length !== 0) {
ajaxOptions.data = {include: sideloads.join(",")};
}

this.deferedFetch = Ember.Resource.ajax(ajaxOptions).done(function(json) {
self.updateWithApiData(json);
});

this.deferedFetch.fail(function() {
Expand All @@ -1074,7 +1082,7 @@ if (typeof this === 'object') this.LRUCache = LRUCache;
self.fetched().reject();
});

this.deferedFetch.success(function() {
this.deferedFetch.done(function() {
self.didFetch.call(self);
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
Expand Down Expand Up @@ -1337,6 +1345,10 @@ if (typeof this === 'object') this.LRUCache = LRUCache;
classOptions.identityMapLimit = options.identityMapLimit;
}

if(options.sideloads) {
classOptions.sideloads = options.sideloads;
}

klass.reopenClass(classOptions);

return klass;
Expand Down
31 changes: 31 additions & 0 deletions spec/javascripts/resourceSpec.js
Expand Up @@ -13,6 +13,37 @@ describe('A Resource instance', function() {

});


describe("defining a resource", function() {

describe("with a sideloads attribute", function() {
var subject;
beforeEach(function() {
subject = Em.Resource.define({
url: "/users",
sideloads: ["abilities", "weapons"]
});
});

it("should not include the sideloads in resourceURL", function() {
var user = subject.create({id: 1});
expect(user.resourceURL()).toEqual("/users/1");
});

it("should send the sideloads in AJAX fetches", function() {
var user = subject.create({id: 1});
spyOn(Em.Resource, 'ajax').andReturn($.when());
user.fetch();
expect(Em.Resource.ajax).toHaveBeenCalledWith({
url: "/users/1",
resource: user,
operation: 'read',
data: {include: "abilities,weapons"}
});
});
});
});

describe('with no ID', function() {

beforeEach(function() {
Expand Down
24 changes: 18 additions & 6 deletions src/ember-resource.js
Expand Up @@ -758,6 +758,7 @@
},

fetch: function() {
var ajaxOptions, sideloads;
if (!Ember.get(this, 'isFetchable')) return $.when();

var url = this.resourceURL();
Expand All @@ -771,13 +772,20 @@
self.willFetch.call(self);
Ember.sendEvent(self, 'willFetch');

this.deferedFetch = Ember.Resource.ajax({
ajaxOptions = {
url: url,
resource: this,
operation: 'read',
success: function(json) {
self.updateWithApiData(json);
}
operation: 'read'
};

sideloads = this.constructor.sideloads;

if(sideloads && sideloads.length !== 0) {
ajaxOptions.data = {include: sideloads.join(",")};
}

this.deferedFetch = Ember.Resource.ajax(ajaxOptions).done(function(json) {
self.updateWithApiData(json);
});

this.deferedFetch.fail(function() {
Expand All @@ -786,7 +794,7 @@
self.fetched().reject();
});

this.deferedFetch.success(function() {
this.deferedFetch.done(function() {
self.didFetch.call(self);
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
Expand Down Expand Up @@ -1049,6 +1057,10 @@
classOptions.identityMapLimit = options.identityMapLimit;
}

if(options.sideloads) {
classOptions.sideloads = options.sideloads;
}

klass.reopenClass(classOptions);

return klass;
Expand Down

0 comments on commit e50cf41

Please sign in to comment.