jashkenas / backbone

Give your JS App some Backbone with Models, Views, Collections, and Events
http://backbonejs.org
MIT License
28.09k stars 5.39k forks source link

Documentation Update Request: idAttribute when using es6 based models #4219

Open gstreetmedia opened 5 years ago

gstreetmedia commented 5 years ago

When using an ES6 class, idAttribute should be returned from a getter, rather than being set in the preinitialize function.

class Meal extends Backbone.Model(
  get idAttribute() {
     return "_id";
  }
);

Otherwise the following will not work.

let model = MealCollection.get(someId);

Due to the use of prototype in the collection function

// Define how to uniquely identify models in the collection.
modelId: function(attrs) {
  return attrs[this.model.prototype.idAttribute || 'id'];
};
jgonggrijp commented 2 years ago

Closely related to #3560 and #4245.