0

According the Backbone.js documentation Model-parse does the following:

parse is called whenever a model's data is returned by the server, in fetch, and save.

To augment models I've already loaded I use Model.parse(). I accomplish this by using fetch to make an additional request for data, then use that data to add properties to an existing model.

Example:

the fetch object is {age: 19}
after the parser will be {age: 19, isAdult: true}

When I perform the save request, in the PUT request I also have other parameters not needed (for example isAdult). I would like to have the original model (without additional parameters in PUT request).

What is the best way to achieve my goal in Backbone?

EBarr
  • 11,826
  • 7
  • 63
  • 85
antonjs
  • 14,060
  • 14
  • 65
  • 91
  • @AntoJs - you're question wasn't very clear. I tried to flush out the idea *i think* you were trying to convey. Make sure my edit was true to your problem. – EBarr May 17 '12 at 21:52
  • @EBarr you got the point of my question. thanks. – antonjs May 18 '12 at 06:07

1 Answers1

2

If I understand your question correctly ....

When backbone talks to a server using a save it sends a complete respresentation of the model. As the docs put it :

The attributes hash (as in set) should contain the attributes you'd like to change — keys that aren't mentioned won't be altered — but, a complete representation of the resource will be sent to the server.

So the default behavior is to send the complete model. If you want to implement you're own logic you're going to have to override the sync method. Dig through the expanded backbone code a bit and you'll see this comment above sync :

// Override this function to change the manner in which Backbone persists
// models to the server. You will be passed the type of request, and the model in question.

I would use the default implementation of sync as my starting point.

EBarr
  • 11,826
  • 7
  • 63
  • 85
  • 1
    `sync` eventually does a `model.toJSON()` to get the `data` for the `$.ajax` call so you could hack up a `sync` on the model that only uses a slice of the normal `toJSON` value. I still think fixing the server code to whitelist all incoming data makes sense but replacing `sync` would also make sense if `isAdult` was a big pile of extra stuff rather than just a simple boolean flag. – mu is too short May 17 '12 at 22:01
  • 1
    @muistooshort - I agree with white listing. Coming from a .net MVC/WebAPI perspective - it's baked into the cake with the model binder. Although, I also understand the instinct to remove extraneous data from calls, particularly if the calls are high frequency (shorter strings to parse server side) or you can prevent many K of data from traversing the wire (lots of unneeded text? mobile where a few K can make a large difference?). – EBarr May 17 '12 at 22:06
  • 1
    For the record, I'm not disagreeing with you. And yes, the bytes do add up pretty quickly in the mobile world. I actually think Backbone should have an `unparse` or `serialize` method that `sync` could use instead of `toJSON`, the default would just `return _.clone(this.attributes)` as `toJSON` does but a distinct method would be nice for this particular use case and offer nice symmetry with the existing `parse`. Maybe I should do a pull request. – mu is too short May 17 '12 at 22:12
  • That sounds like a workable solution to me. I know I would utilize it. – EBarr May 17 '12 at 22:44