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

parse / toJSON applied inconsistent #111 #113

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.floo
*map
.idea*
1 change: 1 addition & 0 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<script type="text/javascript" src="spec/backbone_extensions_spec.js"></script>
<script type="text/javascript" src="spec/integration_spec.js"></script>
<script type="text/javascript" src="spec/bugs_spec.js"></script>
<script type="text/javascript" src="spec/parse_specs.js"></script>

<script type="text/javascript">
(function() {
Expand Down
27 changes: 20 additions & 7 deletions backbone.dualstorage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@ localsync = (method, model, options) ->
store.clean(model, 'dirty')
when 'delete'
store.destroy(model)
if options.dirty
if options.dirty && !model.hasTempId()
store.destroyed(model)
else
if model.id.toString().length == 36
store.clean(model, 'dirty')
else
store.clean(model, 'destroyed')
response = response.attributes if response?.attributes

# Returne the serialized form of the model, this form is passed to parse
response = response.toJSON() if response?.attributes

unless options.ignoreCallbacks
if response
Expand All @@ -225,14 +227,19 @@ localsync = (method, model, options) ->
# Helper function to run parseBeforeLocalSave() in order to
# parse a remote JSON response before caching locally
parseRemoteResponse = (object, response) ->
if not (object and object.parseBeforeLocalSave) then return response
if _.isFunction(object.parseBeforeLocalSave) then object.parseBeforeLocalSave(response)
if _.isFunction(object.parseBeforeLocalSave)
object.parseBeforeLocalSave response
else
response

modelUpdatedWithResponse = (model, response) ->
modelClone = new Backbone.Model
# Use the methods from the model to support "inheritance"
modelClone.toJSON = _.bind(model.toJSON, model);
modelClone.parse = _.bind(model.parse, model);
modelClone.idAttribute = model.idAttribute
modelClone.set model.attributes
modelClone.set model.parse response
modelClone.set modelClone.parse response
modelClone

backboneSync = Backbone.sync
Expand Down Expand Up @@ -278,18 +285,23 @@ dualsync = (method, model, options) ->
success localsync(method, model, options)
else
options.success = (resp, status, xhr) ->
# for backwards compatiblity, should not be needed anymore
resp = parseRemoteResponse(model, resp)

if model instanceof Backbone.Collection
collection = model

# parse the model using the parse method of the collection
# can be used to adjust the schema of collection elements (not for a single element)
workingResponse = collection.parse(resp)
idAttribute = collection.model.prototype.idAttribute
localsync('clear', collection, options) unless options.add
for modelAttributes in resp
for modelAttributes in workingResponse
model = collection.get(modelAttributes[idAttribute])
if model
responseModel = modelUpdatedWithResponse(model, modelAttributes)
else
responseModel = new collection.model(modelAttributes)
responseModel = new collection.model(modelAttributes, { parse: true })
localsync('update', responseModel, options)
else
responseModel = modelUpdatedWithResponse(model, resp)
Expand Down Expand Up @@ -340,6 +352,7 @@ dualsync = (method, model, options) ->

when 'delete'
if model.hasTempId()
options.ignoreCallbacks = false
localsync(method, model, options)
else
options.success = (resp, status, xhr) ->
Expand Down
25 changes: 15 additions & 10 deletions backbone.dualstorage.js

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

Loading