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 3 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 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
21 changes: 15 additions & 6 deletions backbone.dualstorage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ localsync = (method, model, options) ->
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,11 +227,13 @@ 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
modelClone = new model.constructor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Backbone.Model here was intentional, because Backbone-relational has problems when there is more than one instance of the same model with the same id (see #75). This avoids that problem by not using the model's constructor which may inherit from the backbone-relational model prototype.

modelClone.idAttribute = model.idAttribute
modelClone.set model.attributes
modelClone.set model.parse response
Expand Down Expand Up @@ -278,18 +282,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)
workingRespone = collection.parse(resp)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this should be workingResponse and not workingRespone?

idAttribute = collection.model.prototype.idAttribute
localsync('clear', collection, options) unless options.add
for modelAttributes in resp
for modelAttributes in workingRespone
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
20 changes: 11 additions & 9 deletions backbone.dualstorage.js

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

Loading