Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
Preserve an offline-saved model's temporary id when updated offline
Browse files Browse the repository at this point in the history
Fixes #145
  • Loading branch information
nilbus committed Aug 12, 2015
1 parent 1528f32 commit d916965
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions backbone.dualstorage.amd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backbone.dualstorage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions backbone.dualstorage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions spec/backbone.dualstorage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions spec/dualsync_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ->
Expand Down
23 changes: 22 additions & 1 deletion spec/dualsync_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion spec/spec_helper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 4 additions & 1 deletion spec/spec_helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d916965

Please sign in to comment.