backbone.syphon icon indicating copy to clipboard operation
backbone.syphon copied to clipboard

Disclude blank values from Syphon.serialize

Open SimplGy opened this issue 12 years ago • 3 comments
trafficstars

Hi Derick!

I'm looking for where to start on changing the serialize behavior so that blank keys are not included in the object that Syphon returns. would changing the default key extractor be the right place to make this adjustment?

Right now I do this outside of Syphon, but I would like to implement it as an option for this library.

getVals: ->
  formData = Syphon.serialize @el
  # remove field names for blank values
  for field, val of formData
    delete formData[field] unless val
  formData

SimplGy avatar Jun 22 '13 01:06 SimplGy

Hi @SimpleAsCouldBe, sorry this wasn't addressed sooner.

I'm tempted to say that this is something that belongs outside of Syphon, but could you provide me a use case for it?

jamiebuilds avatar Apr 23 '14 02:04 jamiebuilds

It's been a long time since I thought about it. I think this applies to any form that the user might fill in partially, depending on needs. So like a profile screen or similar. If Syphon let me just ignore blank fields instead of including blank values it'd be a better/smaller JSON package.

SimplGy avatar Apr 23 '14 05:04 SimplGy

What should Syphon do with blank fields?

If they're included, then when the caller performs a model.set they're potentially going to wipe out some other fields that they're wanting to hold onto. However perhaps that's the desired behaviour.

My thought here is to expand the serialize API to allow for conditional blank key inclusion.

Thoughts?

rhubarbselleven avatar Feb 24 '15 02:02 rhubarbselleven