Skip to content

Commit

Permalink
ResourceCollection#fetch: short-circuit when prePopulated. Closes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
James A. Rosen committed Apr 18, 2012
1 parent 73c8932 commit aac5ac6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
24 changes: 11 additions & 13 deletions dist/ember-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -1368,24 +1368,22 @@ if (typeof this === 'object') this.LRUCache = LRUCache;
},

fetch: function() {
if (!Ember.get(this, 'isFetchable')) return $.when();
if (!Ember.get(this, 'isFetchable') || Ember.get(this, 'prePopulated')) return $.when();

if (!this.prePopulated) {
var self = this;
var self = this;

if (this.deferedFetch && !Ember.get(this, 'isExpired')) return this.deferedFetch;
if (this.deferedFetch && !Ember.get(this, 'isExpired')) return this.deferedFetch;

Ember.sendEvent(self, 'willFetch');
Ember.sendEvent(self, 'willFetch');

this.deferedFetch = this._fetch(function(json) {
Ember.set(self, 'content', self.parse(json));
});
this.deferedFetch = this._fetch(function(json) {
Ember.set(self, 'content', self.parse(json));
});

this.deferedFetch.always(function() {
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
});
}
this.deferedFetch.always(function() {
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
});
return this.deferedFetch;
},
_resolveType: function() {
Expand Down
21 changes: 21 additions & 0 deletions spec/javascripts/resourceCollectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,25 @@ describe('ResourceCollection', function() {
expect(collection.resolveUrl()).toEqual('/url/from/collection');
});

describe("when prepopulated", function() {

beforeEach(function() {
this.collection = Em.ResourceCollection.create({
type: Object,
content: [ { name: 'hello' } ]
});
});

it('knows it is prePopulated', function() {
expect(this.collection.get('prePopulated')).toBeTruthy();
});

it('returns a resolved deferred for #fetch', function() {
var result = this.collection.fetch();
expect(result).not.toBeUndefined();
expect(result.isResolved()).toBeTruthy();
});

});

});
24 changes: 11 additions & 13 deletions src/ember-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,24 +1080,22 @@
},

fetch: function() {
if (!Ember.get(this, 'isFetchable')) return $.when();
if (!Ember.get(this, 'isFetchable') || Ember.get(this, 'prePopulated')) return $.when();

if (!this.prePopulated) {
var self = this;
var self = this;

if (this.deferedFetch && !Ember.get(this, 'isExpired')) return this.deferedFetch;
if (this.deferedFetch && !Ember.get(this, 'isExpired')) return this.deferedFetch;

Ember.sendEvent(self, 'willFetch');
Ember.sendEvent(self, 'willFetch');

this.deferedFetch = this._fetch(function(json) {
Ember.set(self, 'content', self.parse(json));
});
this.deferedFetch = this._fetch(function(json) {
Ember.set(self, 'content', self.parse(json));
});

this.deferedFetch.always(function() {
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
});
}
this.deferedFetch.always(function() {
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
});
return this.deferedFetch;
},
_resolveType: function() {
Expand Down

0 comments on commit aac5ac6

Please sign in to comment.