dev.pus
dev.pus

Reputation: 8139

Backbone.js sents POST instead of PUT (mongoose set _id to id)

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'

Upvotes: 1

Views: 716

Answers (2)

Jay Kumar
Jay Kumar

Reputation: 1534

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';

Upvotes: 0

nikoshr
nikoshr

Reputation: 33364

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.

Upvotes: 6

Related Questions