Reputation: 483
I have the following code :
this.myModel.save(this.patchAttributes, {
//Ignored if saving new record; will send a POST
patch: true,
success: function() {
success();
}
},
error: function() {
error();
}
});
If my patchAttributes has an entry like fieldName:undefined
, I cannot see it in the request headers of the PATCH request.
Upvotes: 3
Views: 267
Reputation: 3354
It is the correct behaviour, take a look at Backbone source code (https://github.com/jashkenas/backbone/blob/master/backbone.js#L1357). And btw, I don't think it is stated explicitly in any of Backbone documentations. It is more like common knowledge when dealing with JSON
If you want to keep those "undefined" variables, you need to set them to, say, null by using defaults
http://backbonejs.org/#Model-defaults
Upvotes: 0
Reputation: 17168
That's because Backbone is JSON.stringifying the data before sending it. The JSON spec does not allow the undefined
token. Therefore any properties that are undefined
will be omitted from the JSON string.
JSON.stringify({ foo: undefined });
// The output is "{}"
Instead of undefined
you should use null
which is part of the JSON spec:
JSON.stringify({ foo: null });
// The output is "{"foo":null}"
Upvotes: 3