具有多种型号的 Backbone Collection? [英] Backbone Collection with multiple models?

查看:18
本文介绍了具有多种型号的 Backbone Collection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Backbone.

I'm learning Backbone.

我想创建一个列表,该列表可以包含具有不同属性的不同模型.

I want to create a list that can contain different models, with different attributes.

例如,以任何顺序列出文件夹内容,其中可以包括类型文件的模型和类型文件夹的模型.

For example, listing folder contents, which could include models of type file and models of type folder, in any order.

file : {
  title : "",
  date : "",
  type : "",
  yaddayadda : ""
}

folder : {
  title : "",
  date : "",
  haminahamina : ""
}

在 Backbone 中表示这一点的正确方法是什么?是否可以有一个包含多个模型的集合?

What is the proper way to represent this in Backbone? Is it possible to have a single Collection with multiple models?

推荐答案

创建一个您的其他模型继承自的基础模型:

Create a base model that your other models inherit from:

var DataModel = Backbone.Model.extend({
    // Whatever you want in here
});

var FileModel = DataModel.extend({
    // Whatever you want in here
});

var FolderModel = DataModel.extend({
    // Whatever you want in here
});

并使集合的 model 类型与基本模型相同:

And make the model type of the collection be that same base model:

var DataCollection = Backbone.Collection.extend({
    model: DataModel
});

这篇关于具有多种型号的 Backbone Collection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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