2

TLDR, question at the bottom. I solved my issue, but after typing all this up, still thought I'd throw out the underlying question

For anyone looking into this error, here's the description/solution:

I'm getting an error when trying to add a View to a Collection:

Uncaught TypeError: Object [object Object] has no method '_validate'

I solved my problem:

I was defining my collection to use a Model that was really a View:

ViewCollection = Backbone.Collection.extend({ model: MyView });

MyView was an instance of Backbone.View, not a model, duh.

Conceptual Question:

Is it OK to use a Backbone.Collection instead of an array or object, to store things, like a Backbone.Collection of Backbone.Views, or any generic array of objects? If my model has multiple views, is it common to store them like so:

model
    collectionOfViews
        view
        view
        view
Michael Lewis
  • 4,252
  • 6
  • 28
  • 39
  • You can make your posts in Q&A style, such as what I did http://stackoverflow.com/questions/14472103/how-to-find-the-minimum-covariant-type-for-best-fit-between-two-types/14472104#14472104 – Ken Kin Oct 30 '13 at 22:23
  • Oh, yes, please don't mix Question and Solution. It's even possible, if you judge so, to change the accepted answer for your own. – brasofilo Oct 30 '13 at 22:48

1 Answers1

0

Because the point of a collection is to query a set of models. That's its entire reason for existence, to filter down a set of data in your model layer. It makes no sense to query views.

The very first sentence in the Backbone documentation on Collection:

Collections are ordered sets of models.

Collection was never intended to be a replacement for array, it's not supposed to wrap a set of arbitrary objects. It wasn't designed or intended or tested to be used in the way you're trying to use it.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • How about as simply a mechanism to make underscore's collection functions object oriented? `_.invoke(collection, 'method')` vs `collection.invoke('method')` – Michael Lewis Oct 30 '13 at 22:25
  • @MikeLewis Use `_.method(array)` if you want to apply some underscore method to an arbitrary array of views. – user229044 Oct 30 '13 at 22:26
  • 1
    A) So what? B) if you want it to by OO, use `_(my_array).method` instead of `_.method(my_array)`. – user229044 Oct 30 '13 at 22:30
  • In my initial comment, it seems like a very good idea to have a generic collection class, that you can use instead of a generic array, for quicker access to those functions. I realize what you're saying would work equally, it just would feel better to me to have it attached to the `collection`, like the `Backbone.Collection` – Michael Lewis Oct 30 '13 at 22:30
  • Then, see B) `_(my_array).method`. `_(my_array)` will return an object that you can invoke arbitrary underscore methods on. – user229044 Oct 30 '13 at 22:31
  • So, rather than `model.views = new ViewCollection();` I could use `model.views = _([]);`, and then say `model.views.add(view)`? Or `model.views.invoke('methodName')`? – Michael Lewis Oct 30 '13 at 22:32
  • You *could*, but why are you trying to do this in the first place? Why do you need a container for your views which is more complex than a simple array? – user229044 Oct 30 '13 at 22:34
  • 1) Its not JUST for views, what if I have an array of some other modules. 2) Its just easier to have all the underscore functions directly attached to the collection, rather than have to write it backwards, starting with `_(`. A minor difference, but organizationally, it seems to make more sense. This is what `Backbone.Collection` does, I'm just surprised there's no generic version. – Michael Lewis Oct 30 '13 at 22:36
  • I'm disappointed this is the case, but thank you for your help. I'll try asking a new question with this new insight. – Michael Lewis Oct 30 '13 at 22:38