Skip to content
Browse files

setting correct resourceState upon ajax failures

  • Loading branch information...
1 parent a47837b commit 1a5a6378a320ce901e86d2beb0a9d5d838583a95 @shajith shajith committed May 25, 2012
Showing with 30 additions and 1 deletion.
  1. +12 −0 spec/javascripts/fetchSpec.js
  2. +18 −1 src/ember-resource.js
View
12 spec/javascripts/fetchSpec.js
@@ -45,6 +45,18 @@ describe('deferred fetch', function() {
server.respondWith('GET', '/people/2', [422, {}, '[["foo", "bar"]]']);
});
+ it('should not prevent subsequent fetches from happening', function() {
+ var resource = Person.create({ id: 2 });
+
+ resource.fetch();
+ server.respond();
+
+ spyOn(resource, 'willFetch').andReturn($.when());
+ resource.fetch();
+ server.respond();
+ expect(resource.willFetch).toHaveBeenCalled();
+ });
+
it('should pass a reference to the resource to the error handling function', function() {
var spy = jasmine.createSpy();
Ember.Resource.errorHandler = function(a, b, c, fourthArgument) {
View
19 src/ember-resource.js
@@ -647,6 +647,11 @@
updateExpiry();
});
+ Ember.addListener(this, 'didFail', this, function() {
+ Ember.set(self, 'resourceState', Ember.Resource.Lifecycle.UNFETCHED);
+ updateExpiry();
+ });
+
var resourceStateBeforeSave;
Ember.addListener(this, 'willSave', this, function() {
resourceStateBeforeSave = Ember.get(self, 'resourceState');
@@ -743,6 +748,7 @@
didFetch: function() {},
willSave: function() {},
didSave: function() {},
+ didFail: function() {},
fetched: function() {
if(!this._fetchDfd) {
@@ -774,12 +780,22 @@
}
});
- this.deferedFetch.always(function() {
+ this.deferedFetch.fail(function() {
+ self.didFail.call(self);
+ Ember.sendEvent(self, 'didFail');
+ self.fetched().reject();
+ });
+
+ this.deferedFetch.success(function() {
self.didFetch.call(self);
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
});
+ this.deferedFetch.always(function() {
+ self.deferedFetch = null;
+ });
+
return this.deferedFetch;
},
@@ -1097,6 +1113,7 @@
this.deferedFetch.always(function() {
Ember.sendEvent(self, 'didFetch');
self.fetched().resolve();
+ self.deferredFetch = null;
});
return this.deferedFetch;
},

0 comments on commit 1a5a637

Please sign in to comment.
Something went wrong with that request. Please try again.