为什么我不能做Backbone.Views的Backbone.Collection? [英] Why can't I make a Backbone.Collection of Backbone.Views?

查看:128
本文介绍了为什么我不能做Backbone.Views的Backbone.Collection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR,在底部的问题。我解决了我的问题,但打字这一切后,还以为我会扔出去的基本问题

对于任何寻找到这个错误,这里的说明/解决方案:

我想一个视图添加到集合时收到一个错误:

 未捕获类型错误:对象的翻译:没有方法'_Validate

我解决我的问题:

我定义我的收藏使用一个模型,是一个真正的景观:

  ViewCollection = Backbone.Collection.extend({模式:MyView的});

MyView的是一个实例 Backbone.View ,不是模特,真不错。

概念性问题:

是否确定要使用的 Backbone.Collection 代替阵列对象,存储的东西,像 Backbone.Collection Backbone.Views 或任何对象的通用阵列?如果我的模型有多个视图,它是常见的把它们存放,像这样:

 模型
    collectionOfViews
        视图
        视图
        视图


解决方案

由于集合点是查询一组模型。这是生存的整个原因,过滤下来的一组数据在模型层。它使的没有任何意义的查询意见。

在主干文档中的第一句对收藏


  

集合是有序的模型集。


收集从未打算成为阵列的替代品,它不是的应该的包裹将任意对象。它不是设计或意图或测试在你试图使用它的方式使用。

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

解决方案

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.

这篇关于为什么我不能做Backbone.Views的Backbone.Collection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆