“Uncaught TypeError: undefined is not a function” initializing Backbone collection

Asked By: Anonymous

I have a Backbone collection something like the following:

var FooCollection = Backbone.Collection.extend({

    initialize: function (attributes, options) {
        this.barId = options.barId;

var Foo = Backbone.Model.extend({});

When I try to initialize this, I get “Uncaught TypeError: undefined is not a function” in the _prepareModel() function of Backbone.Collection.

The bad call is in model = new this.model(attrs, options).

// Prepare a model or hash of attributes to be added to this collection.
_prepareModel: function(model, options) {
  options || (options = {});
  if (!(model instanceof Model)) {
    var attrs = model;
    options.collection = this;
    model = new this.model(attrs, options); // <-- BLOWS UP HERE
    if (!model._validate(model.attributes, options)) model = false;
  } else if (!model.collection) {
    model.collection = this;
  return model;

When I step through _prepareModel() in the debugger, it looks like the type of this at that point is child, and this.model is, in fact, undefined.

Can anyone tell me what I’m doing wrong?


Answered By: Anonymous

In my actual code Foo was declared after FooCollection. Didn’t realize that Javascript doesn’t support forward declarations. [headdesk]

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen Windows 10 Lisans Office 2019 Mac Satın Al follower kaufen instagram follower kaufen porno