Permalink
Browse files

Preserve an offline-saved model's temporary id when updated offline

Fixes #145
  • Loading branch information...
1 parent 1528f32 commit d916965d0fd1a4c8d931e55e25ed67ae843e8cdf @nilbus committed Aug 12, 2015
View
@@ -1,3 +1,7 @@
+### master / unreleased
+
+* Fix #145: Prevent model's id from being nulled (and saved locally with that id) when an offlineStatusCode is returned; broken since 1.4.0
+
### 1.4.0 / 2015-02-16
* Fix #123: Expose original BackboneSync as Backbone.DualStorage.originalSync to allow custom online sync methods (Richard Tibbles)
@@ -435,13 +435,13 @@ dualsync = function(method, model, options) {
temporaryId = model.id;
options.success = function(resp, _status, _xhr) {
var updatedModel;
+ model.set(model.idAttribute, temporaryId, {
+ silent: true
+ });
if (hasOfflineStatusCode(options.xhr)) {
return useOfflineStorage();
}
updatedModel = modelUpdatedWithResponse(model, resp);
- model.set(model.idAttribute, temporaryId, {
- silent: true
- });
localsync('delete', model, options);
localsync('create', updatedModel, options);
return success(resp, _status, _xhr);
@@ -332,9 +332,9 @@ dualsync = (method, model, options) ->
temporaryId = model.id
options.success = (resp, _status, _xhr) ->
+ model.set model.idAttribute, temporaryId, silent: true
return useOfflineStorage() if hasOfflineStatusCode options.xhr
updatedModel = modelUpdatedWithResponse model, resp
- model.set model.idAttribute, temporaryId, silent: true
localsync('delete', model, options)
localsync('create', updatedModel, options)
success(resp, _status, _xhr)
@@ -428,13 +428,13 @@ as that.
temporaryId = model.id;
options.success = function(resp, _status, _xhr) {
var updatedModel;
+ model.set(model.idAttribute, temporaryId, {
+ silent: true
+ });
if (hasOfflineStatusCode(options.xhr)) {
return useOfflineStorage();
}
updatedModel = modelUpdatedWithResponse(model, resp);
- model.set(model.idAttribute, temporaryId, {
- silent: true
- });
localsync('delete', model, options);
localsync('create', updatedModel, options);
return success(resp, _status, _xhr);
@@ -426,13 +426,13 @@ dualsync = function(method, model, options) {
temporaryId = model.id;
options.success = function(resp, _status, _xhr) {
var updatedModel;
+ model.set(model.idAttribute, temporaryId, {
+ silent: true
+ });
if (hasOfflineStatusCode(options.xhr)) {
return useOfflineStorage();
}
updatedModel = modelUpdatedWithResponse(model, resp);
- model.set(model.idAttribute, temporaryId, {
- silent: true
- });
localsync('delete', model, options);
localsync('create', updatedModel, options);
return success(resp, _status, _xhr);
@@ -251,6 +251,17 @@ describe 'offline storage', ->
expect(localsync.calls.length).toEqual 1
expect(localsync.calls[0].args[2].dirty).toBeTruthy()
+ it "preserves an offline-saved model's temporary id when updated offline", ->
+ ready = undefined
+ temporaryId = 'tttttttttttttttttttttttttttttttttttt'
+ runs ->
+ ready = false
+ model.set model.idAttribute, temporaryId
+ dualsync('update', model, success: (-> ready = true), successStatus: 0)
+ waitsFor (-> ready), "The success callback should have been called", 100
+ runs ->
+ expect(model.id).toEqual temporaryId
+
describe 'dualStorage hooks', ->
beforeEach ->
model.parseBeforeLocalSave = ->
View
@@ -486,7 +486,7 @@
});
describe('offline storage', function() {
- return it('marks records dirty when options.remote is false, except if the model/collection is marked as local', function() {
+ it('marks records dirty when options.remote is false, except if the model/collection is marked as local', function() {
var ready;
spyOnLocalsync();
ready = void 0;
@@ -528,6 +528,27 @@
return expect(localsync.calls[0].args[2].dirty).toBeTruthy();
});
});
+ return it("preserves an offline-saved model's temporary id when updated offline", function() {
+ var ready, temporaryId;
+ ready = void 0;
+ temporaryId = 'tttttttttttttttttttttttttttttttttttt';
+ runs(function() {
+ ready = false;
+ model.set(model.idAttribute, temporaryId);
+ return dualsync('update', model, {
+ success: (function() {
+ return ready = true;
+ }),
+ successStatus: 0
+ });
+ });
+ waitsFor((function() {
+ return ready;
+ }), "The success callback should have been called", 100);
+ return runs(function() {
+ return expect(model.id).toEqual(temporaryId);
+ });
+ });
});
describe('dualStorage hooks', function() {
@@ -5,8 +5,10 @@ window.Backbone.sync = jasmine.createSpy('sync').andCallFake (method, model, opt
callback = options.success
xhr = {status: status, response: resp}
if typeof options.errorStatus is 'number'
- resp.status = status = options.errorStatus
+ resp.status = xhr.status = status = options.errorStatus
callback = options.error
+ if typeof options.successStatus is 'number'
+ resp.status = xhr.status = status = options.successStatus
if Backbone.VERSION == '0.9.10'
callback(model, resp, options)
else if Backbone.VERSION[0] == '0'
View
@@ -11,9 +11,12 @@
response: resp
};
if (typeof options.errorStatus === 'number') {
- resp.status = status = options.errorStatus;
+ resp.status = xhr.status = status = options.errorStatus;
callback = options.error;
}
+ if (typeof options.successStatus === 'number') {
+ resp.status = xhr.status = status = options.successStatus;
+ }
if (Backbone.VERSION === '0.9.10') {
callback(model, resp, options);
} else if (Backbone.VERSION[0] === '0') {

0 comments on commit d916965

Please sign in to comment.