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

Couldn't construct model, with polymorphic models #4233

Closed ogonkov closed 2 years ago

ogonkov commented 4 years ago

Following code is throwing TypeError

class MyModel extends Backbone.Model {
  validate(attributes) {
    return null;
  }
}

class MyCollection extends Backbone.Collection {
  modelId(attr) {
    return attr.x;
  } 

  model(attributes, options) {
    return new MyModel(attributes, options);
  }
}

try {
const inst = new MyCollection([{a: 2}]);
} catch(ex) {
  alert(ex)
}
TypeError: this.model is not a constructor
    at MyCollection._prepareModel (VM16 backbone.js:1137)
    at MyCollection.set (VM16 backbone.js:889)
    at MyCollection.add (VM16 backbone.js:814)
    at MyCollection.reset (VM16 backbone.js:960)
    at new Backbone.Collection (VM16 backbone.js:765)
    at new MyCollection ((index):40)
    at window.onload ((index):51)

https://jsfiddle.net/ogonkov/ng19ae6d/

I think it have same root with #4203