Backbone Collection有多个型号? [英] Backbone Collection with multiple models?

查看:180
本文介绍了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
});

并制作模型类型的集合是相同的基本模型:

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天全站免登陆