2

I am working on a project using the following namespacing scheme:

var abc = {};
abc.models = {};
abc.views = {};

abc.models.Collection = Backbone.Collection.extend({});

When I seek to create a new collection I receive the following error

var collection = new abc.models.Collection({this:"that"});
TypeError: Object #<Object> has no method '_reset'

when I moved the constructor to abc.Collection the error stopped and everything started working as needed.

Can someone please help me understand why this is?

I should also add that this is part of a multi-file application where each file is wrapped with the following:

(function($, abc){

//module contents here

}(jQuery, abc));
5
  • Works for me, check the working jsFiddle the issue should be somewhere else. Which Backbone version are you using? Commented Aug 21, 2012 at 14:32
  • Same version than in my jsFiddle.. check it out, why is working there? Commented Aug 21, 2012 at 14:49
  • That's interesting. It could be there is something funky happening in the abc namespace that is messing with things. I think for now I will make shallow these constructors. Commented Aug 21, 2012 at 14:54
  • There is an internal _reset method in Backbone.Collection but there's not enough information here to know what's going wrong. Commented Aug 21, 2012 at 17:46
  • I've been doing a lot of refactoring today. It appears the issues have cleared up. I just wish I knew exactly why. Thanks for you input. Commented Aug 21, 2012 at 21:58

2 Answers 2

3

this works:

var abc = {};
abc.models = {};
abc.views = {};
abc.models.Collection = Backbone.Collection.extend({});
var collection = new abc.models.Collection([], {});

whereas this trips with Uncaught TypeError: Object #<Object> has no method '_reset':

var abc = {};
abc.models = {};
abc.views = {};
abc.models.Collection = Backbone.Collection.extend({});
var collection = abc.models.Collection([], {});

The difference is not calling the constructor with new.

Sign up to request clarification or add additional context in comments.

Comments

0

The first argument to a new Backbone Collection is an array of models and then a hash of options. Are you trying to pass in an array of models, if not then pass in the empty array: []

1 Comment

That's not the issue. You can pass a simple object and if there is a model assigned to the collection, it will create the model on the fly. The question here is why does the creation before fail when the Constructor is 2 levels deep into an object as opposed to 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.