首页 > 其他分享 >ExtJS-Data Package (数据处理包) - Model间关系

ExtJS-Data Package (数据处理包) - Model间关系

时间:2023-04-04 21:03:28浏览次数:67  
标签:model name Package Data Ext Model type id

更新记录
2023年3月9日 发布。

ExtJS教程汇总:https://www.cnblogs.com/cqpanda/p/16328016.html

官方文档:https://docs.sencha.com/extjs/7.6.0/classic/Ext.data.schema.Association.html

说明

在ExtJS中,可以设置模型与模型之间的关系,如一对多、多对一或一对一关系。
定义Model关系的方式:
1、使用hasMany/hasOne/belongsTo配置项即可。比如:

hasMany: { model: 'xx', name: ''},
hasOne: { model: 'xx', name: ''},
belongsTo: '',

2、除了使用直接配置项,还可以使用associations配置项,比如:

associations: {type: 'hasOne', model: 'xxx', name: ''}

一对多

说明

使用hashMany配置项即可。

实例:一个用户有多个文章数据

Ext.define('User',{
    extend: 'Ext.data.Model',
    field: [ 'id' ],
    hasMany: [
        //在用户模型与文章模型之间建立一对多的关联关系
        { model: 'Article', name: 'articles' },
    ]
});

Ext.define('Article',{
    extend: 'Ext.data.Model',
    field: [ 'id' ],
    //文章对应属于一个用户
    belongsTo: 'User',
});

实例:一个用户有多条评论数据

Ext.define('User',{
    extend: 'Ext.data.Model',
    field: [ 'id' ],
    hasMany: [
        //在用户模型与评论模型之间建立一对多的关联关系
        {model: 'Comment', name: 'comments' }
    ]
});

实例: 一篇文章有多条评论数据

Ext.define('Article',{
    extend: 'Ext.data.Model',
    field: [ 'id' ],
    //在文章模型与评论模型之间建立一对多的关联关系
    hasMany: { model: 'Comment', name: 'comments' }
});


Ext.define('Comment',{
    extend: 'Ext.data.Model',
    field: [ 'id' ],
    //评论属于文章
    belongsTo: 'Article',
});

(待优化)实例:模型实例化(一对一关联,One-to-one associations)

//被引用的Model
Ext.define('Myapp.model.Contract',{
    extend:'Ext.data.Model',
    idProperty:'id ',
    fields: [
        {name: 'id', type: 'int' },
        {name: 'contractId', type: 'string'},
        {name: 'documentType', type: 'string'}
    ]
});

//引用其他Model的Model
Ext.define('Myapp.model.Customer',{
    extend:'Ext.data.Model',
    requires: [ 'Myapp.model.Contract'],
    idProperty:'id ',fields:[
        {name: 'id', type: 'int'},
        {name: 'name'    , type: 'string'},
        {name: 'phone'   , type: 'string'},
        {name: 'website' , type: 'string'},
        {name: 'status'  , type: 'string'},
        {name: 'clientSince' , type: 'date', dateFormat: 'Y-m-d H:i'},
        {name: 'contractInfo' , reference: 'Contract', unique:true}
   ]
});

//创建Model实例
let myclient = Ext.create('Myapp.model.Customer',{
    id: 10001,
    name: 'Acme corp',
    phone: '+52-01-55-4444-3210',
    website: 'www.acmecorp.com',
    status: 'Active',
    clientSince: '2010-01-01 14:35',
    //为引用的Model赋值
    contractInfo:{
        id:444,
        contractId:'ct-001-444',
        documentType:'PDF'
    }
});

(待优化)实例:模型实例化(一对一关联,One-to-one associations)

//被引用的Model
Ext.define('Myapp.model.Contract',{
    extend:'Ext.data.Model',
    idProperty:'id ',
    fields: [
        {name: 'id', type: 'int' },
        {name: 'contractId', type: 'string'},
        {name: 'documentType', type: 'string'}
    ]
});

//引用其他Model的Model
Ext.define('Myapp.model.Customer',{
    extend:'Ext.data.Model',
    requires: [ 'Myapp.model.Contract'],
    idProperty:'id ',fields:[
        {name: 'id', type: 'int'},
        {name: 'name'    , type: 'string'},
        {name: 'phone'   , type: 'string'},
        {name: 'website' , type: 'string'},
        {name: 'status'  , type: 'string'},
        {name: 'clientSince' , type: 'date', dateFormat: 'Y-m-d H:i'},
        {name: 'contractInfo' , reference: 'Contract', unique:true}
   ]
});

//创建Model实例
let myclient = Ext.create('Myapp.model.Customer',{
    id: 10001,
    name: 'Acme corp',
    phone: '+52-01-55-4444-3210',
    website: 'www.acmecorp.com',
    status: 'Active',
    clientSince: '2010-01-01 14:35',
    //为引用的Model赋值
    contractInfo:{
        id:444,
        contractId:'ct-001-444',
        documentType:'PDF'
    }
});

(待优化) 实例:一对多关联(One-to-many associations)

Ext.define('Myapp.model.Client',{
    extend:'Ext.data.Model',
    //加载依赖的Model
    requires: ['Myapp.model.Employee'],
    idProperty:'id ',
    fields:[....  ],
    //配置一对多
    hasMany:{
        model:'Myapp.model.Employee',
        name:'employees',
        associationKey: 'employees'
    }
});

//被引用的Model
Ext.define('Myapp.model.Employee',{
    extend:'Ext.data.Model',
    idProperty:'id',
    fields:[
        {name: 'id', type: 'int' },
        {name: 'clientid'  , type: 'int'},
        {name: 'name'      , type: 'string'},
        {name: 'phone'     , type: 'string'},
        {name: 'email'     , type: 'string'},
        {name: 'gender'    , type: 'string'}
    ]
});


//model实例化
let myclient = Ext.create('Myapp.model.ClientWithContacts',{
    id: 10001,
    name: 'Acme corp',
    phone: '+52-01-55-4444-3210',
    website: 'www.acmecorp.com',
    status: 'Active',
    clientSince: '2010-01-01 14:35'
});

//给Model引用的Model添加数据
myclient.employees().add(
    {
        id:101,
        clientId:10001,
        name:'Juan Perez',
        phone:'+52-05-2222-333',
        email:'[email protected]',
        gender:'male'
    },
    {
        id:102,
        clientId:10001,
        name:'Sonia Sanchez',
        phone:'+52-05-1111-444',
        email:'[email protected]',
        gender:'female'
    }
);

//遍历读取被引用的Model
myclient.employees().each(function(record){  
    console.log(record.get('name') + ' - ' + record.get('email') );
});

标签:model,name,Package,Data,Ext,Model,type,id
From: https://www.cnblogs.com/cqpanda/p/17198416.html

相关文章

  • Jetpack—LiveData组件的缺陷以及应对策略
    一、前言为了解决Android-App开发以来一直存在的架构设计混乱的问题,谷歌推出了Jetpack-MVVM的全家桶解决方案。作为整个解决方案的核心-LiveData,以其生命周期安全,内存安全等优点,甚至有逐步取代EventBus,RxJava作为Android端状态分发组件的趋势。官网商城app团队在深度使用LiveData的......
  • oracle之安装data gaurd集群
    #################### 开启归档日志:shutdownimmediate;startupmount;alterdatabasearchivelog;alterdatabaseopen;archiveloglist#更改归档目录路径log_archive_dest_naltersystemsetlog_archive_dest_1='location=D:\software\arch1\';#手动切换日......
  • BOM(Browser Object Model)对象模型
    ?window对象是全局对象,基本BOM的属性和方法都是window的window属性和方法属性方法点击某按钮,回到顶部window.scrollTo(0,0)......
  • DataLeap 数据资产实战:如何实现存储优化?
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群背景DataLeap作为一站式数据中台套件,汇集了字节内部多年积累的数据集成、开发、运维、治理、资产、安全等全套数据中台建设的经验,助力企业客户提升数据研发治理效率、降低管理成本。Da......
  • 如何找出 SAP Fiori Launchpad 里点击 tile 之后,读取业务数据调用的是哪个 SAP 后台系
    笔者曾经写过一篇文章SAPFiori应用的三种部署方式,里面介绍了SAPFiori应用部署的一种典型方式:Fiori应用的载体即SAPUI5应用,部署在Gateway系统上,也称FrontendServer(前台服务器),如下图红色方框高亮所示。当用户访问FioriLaunchpad代表SAPUI5应用的一个个tile......
  • 在SpringDataJPA中使用Querydsl(kotlin版)
    前言我们在做日常开发中经常会进行数据库的操作,ORM框架可以帮助我们更便捷的进行数据的操作。SpringDataJPA就是我们经常用到的ORM框架,我们只需要定义一些实体类以及实现一些接口,它便为我们生成了一些丰富的SQL操作功能。但是如果涉及到多表动态查询,JPA的功能就显得有些捉襟见......
  • Cannot download Packages/expat-devel-2.2.5-4.el8.x86_64.rpm: All mirrors were tr
    错误原因从错误可以看出无法下载此包,因为所有镜像都已经尝试过了。可能是因为该软件包不再可用或镜像服务器当前不可用。解决方法因为CENTOS8自带rpm,所以就不需要下载rpm了。检查依赖包是否安装:(这步可忽略)rpm-qmakeautoconfautomakecmakeperl-CPANlibcurl-develli......
  • c++ std::package_task,task.get_future()
    #include<iostream>#include<future>#include<thread>intcountdown(intfrom,intto){for(inti=from;i!=to;--i){std::cout<<i<<std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));......
  • pandas dataframe使用方法
    目录基本读写:数据可视化基本读写:使用PandasDataFrame的步骤如下:导入Pandas模块pythonimportpandasaspd创建DataFramepythondf=pd.DataFrame({ '姓名':['张三','李四','王五'], '年龄':[18,25,30], '性别':['男&#......
  • AnnData
    AnnData是一个用于单细胞数据分析的Python类,由Scanpy团队开发。它主要用于存储和处理单细胞RNA-seq数据,并提供了丰富的功能和方法来进行数据预处理、分析和可视化。AnnData对象中存储的是基因表达矩阵(GeneExpressionMatrix)及其相关的注释信息,例如细胞类型、样本信息、基因注释......