-
Notifications
You must be signed in to change notification settings - Fork 108
Added option to allow localStorage to update first and return successful immediately. #86
base: master
Are you sure you want to change the base?
Changes from 1 commit
cc9a73e
8ffdc68
617fae2
2649a6a
23e59d7
63f9001
c93f4d9
1cc56ab
9236c16
11e2a2b
163a2f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ Backbone.Collection.prototype.syncDirty = -> | |
|
||
for id in ids | ||
model = if id.length == 36 then @findWhere(id: id) else @get(id) | ||
model.remoteFirst = true if (result(model, 'localFirst') or result(model.collection, 'localFirst')) | ||
model?.save() | ||
|
||
Backbone.Collection.prototype.syncDestroyed = -> | ||
|
@@ -27,6 +28,7 @@ Backbone.Collection.prototype.syncDestroyed = -> | |
for id in ids | ||
model = new @model(id: id) | ||
model.collection = @ | ||
model.remoteFirst = true if (result(model, 'localFirst') or result(model.collection, 'localFirst')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or this:
There are lots of ways to skin a cat. I don't have a strong preference in this case. |
||
model.destroy() | ||
|
||
Backbone.Collection.prototype.syncDirtyAndDestroyed = -> | ||
|
@@ -213,6 +215,14 @@ parseRemoteResponse = (object, response) -> | |
if not (object and object.parseBeforeLocalSave) then return response | ||
if _.isFunction(object.parseBeforeLocalSave) then object.parseBeforeLocalSave(response) | ||
|
||
# Right now Backbone.dualStorage infers a model is persisted if | ||
# it's id length is NOT 36 characters | ||
isModelPersisted = (model) -> | ||
return not (_.isString(model.id) and model.id.length == 36) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that #83 has been merged, this conflicts and needs to be merged with master to be resolved. This method is replaced by |
||
|
||
isLocallyCached = (storeName) -> | ||
return localStorage.getItem(storeName); | ||
|
||
modelUpdatedWithResponse = (model, response) -> | ||
modelClone = new Backbone.Model | ||
modelClone.idAttribute = model.idAttribute | ||
|
@@ -231,18 +241,34 @@ dualsync = (method, model, options) -> | |
result(model.collection, 'url') || result(model, 'urlRoot') || result(model, 'url') | ||
options.success = callbackTranslator.forDualstorageCaller(options.success, model, options) | ||
options.error = callbackTranslator.forDualstorageCaller(options.error, model, options) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing white space. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does need to go. |
||
# execute only online sync | ||
return onlineSync(method, model, options) if result(model, 'remote') or result(model.collection, 'remote') | ||
|
||
# execute localSyncFirst but skip if 'localFirst' has been explicitly set to false | ||
# TO DO: This check seems smelly. REFACTOR. | ||
#if not (result(model, 'localFirst') == false || result(model.collection, 'localFirst') == false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has this already been refactored, and these are old comments? |
||
return localSyncFirst(method, model, options) if (result(model, 'localFirst') or result(model.collection, 'localFirst')) and not result(model, 'remoteFirst') | ||
|
||
# execute only local sync | ||
local = result(model, 'local') or result(model.collection, 'local') | ||
options.dirty = options.remote is false and not local | ||
return localsync(method, model, options) if options.remote is false or local | ||
|
||
# execute dual sync | ||
# Execute remoteSyncFirst | ||
return remoteSyncFirst(method,model,options) | ||
|
||
localSyncFirst = (method, model, options) -> | ||
if not isLocallyCached(options.storeName) then return remoteSyncFirst(method, model, options) | ||
options.dirty = true; | ||
return localsync(method, model, options); | ||
|
||
remoteSyncFirst = (method, model, options) -> | ||
# execute standard dual sync | ||
options.ignoreCallbacks = true | ||
|
||
delete model.remoteFirst if model.remoteFirst? | ||
|
||
success = options.success | ||
error = options.error | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of setting an attribute on the model, what do you think about passing in a special option to save, such as
save(remoteFirst: true)
? It should get passed through to sync/dualStorage. Then you also won't have to clean it up later.