1

Backbone.js sents an POST on updated objects instead of an PUT.

I think this is because mongoDB uses _id instead of id.

How do I say backbone.js to use _id instead of id?

I already tried some server-side change but I think it would be easier just to say that backbone should use another attribute as id. MongoDB: output 'id' instead of '_id'

Community
  • 1
  • 1
dev.pus
  • 7,919
  • 13
  • 37
  • 51

2 Answers2

6

From the fine manual http://backbonejs.org/#Model-idAttribute

idAttribute model.idAttribute

A model's unique identifier is stored under the id attribute. If you're directly communicating with a backend (CouchDB, MongoDB) that uses a different unique key, you may set a Model's idAttribute to transparently map from that key to id.

nikoshr
  • 32,926
  • 33
  • 91
  • 105
0

Id attribute can be changed per model like

var Meal = Backbone.Model.extend({
  idAttribute: "_id"
});

To make it default for all models (eg., if we are using mongodb), override the default setting by placing this LOC in your app js file that runs after backbone.js

//override the id attribute for model
Backbone.Model.prototype.idAttribute = '_id';
Jay Kumar
  • 1,514
  • 1
  • 14
  • 17