Skip to content

Commit ff828fc

Browse files
committed
refs nilbus#16 - use localStorage before remotesync completes for gets. adds shouldRemoteSync option
1 parent 0bede52 commit ff828fc

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

backbone.dualstorage.coffee

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ dualsync = (method, model, options) ->
205205
console.log "can't clear", options.storeName, "require sync dirty data first"
206206
success localsync(method, model, options)
207207
else
208-
options.success = (resp, status, xhr) ->
208+
remoteOptions = _(options).clone()
209+
remoteOptions.success = (resp, status, xhr) ->
209210
console.log 'got remote', resp, 'putting into', options.storeName
210211
resp = parseRemoteResponse(model, resp)
211212

@@ -218,13 +219,24 @@ dualsync = (method, model, options) ->
218219
else
219220
localsync('create', resp, options)
220221

221-
success(resp, status, xhr)
222+
success(resp, status, xhr) # ideally this would only be called if we knew that it was different from localStorage, instead of reset()-ing
222223

223-
options.error = (resp) ->
224-
console.log 'getting local from', options.storeName
225-
success localsync(method, model, options)
226-
227-
onlineSync(method, model, options)
224+
remoteOptions.error = (resp) ->
225+
console.log('remote sync failed, doing nothing')
226+
227+
# after localsync loads, check to see if we should still remotesync
228+
options.ignoreCallbacks = false
229+
options.success = (resp) ->
230+
# first fire the success callback which will populate the model
231+
success(resp)
232+
233+
# now that the collection is populated from localStorage, check if we should remote
234+
if _.isUndefined(model.shouldRemoteSync) or (_.isFunction(model.shouldRemoteSync) and model.shouldRemoteSync())
235+
onlineSync(method, model, remoteOptions)
236+
237+
238+
console.log 'getting local from', options.storeName
239+
localsync(method, model, options)
228240

229241
when 'create'
230242
options.success = (resp, status, xhr) ->

backbone.dualstorage.js

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)