Extjs中Ext.data.Model的schema配置项

本文介绍了在 ExtJS 中如何使用 Ext.data.Model 的 schema 配置,包括基类与子类的关系、entityName 的生成规则、namespace 的影响以及 schema 在 proxy API 地址动态生成和请求参数添加中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、首先看例子:
Ext.define(‘Ims.model.BaseDataModel’, {
extend: ‘Ext.data.Model’,

requires: [
    'Ims.ux.data.proxy.Format',
    'Ext.data.identifier.Negative'
],

identifier: 'negative',

idProperty: 'id',

fields: [
    {name: 'id', type: 'int'}
],

schema: {  //schema用于基类,尤其是配置namespace这个项时
    namespace: 'Ims.model',
    urlPrefix: Ims.Config.server.contextPath,
    proxy: {
        type: 'format',

        api: {
            read: '{prefix}/{entityName:lowercase}/get',      //entityName在子类中产生,example: sys/menu,子类中可以配置entityName项,也可以不配置而自动产生
            create: '{prefix}/{entityName:lowercase}/create',
            update: '{prefix}/{entityName:lowercase}/update',
            destroy: '{prefix}/{entityName:lowercase}/delete'
        }
    }
}

});

子类Menu继承自baseModel:
Ext.define(‘Ims.model.sys.Menu’, {
extend: ‘Ims.model.BaseDataModel’,

alias: 'model.sysmenu',

requires: [
    'Ext.data.validator.Length',
    'Ext.data.validator.Presence'
],

entityName: 'sys/menu',

fields: [
    {name: 'parentId', type: 'int'},
    {name: 'text', type: 'string'},
    {name: 'sort', type: 'int'},
    {name: 'type', type: 'string'},
    {name: 'href', type: 'string'},
    {name: 'target', type: 'string'},
    {name: 'iconCls', type: 'string'},
    {name: 'isShow', type: 'string', defaultValue: '1'},
    {name: 'permission', type: 'string'},
    {name: 'description', type: 'string'},
    {name: 'remarks', type: 'string'},
    {name: 'isNewRecord', type: 'boolean', defaultValue: true}
],

validators: {
    parentId: [{
        type: 'presence', message: '必须选择上级菜单'
    }],

    text: [{
        type: 'presence', message: '必须填写菜单名称'
    }, {
        type: 'length', min: 2, minOnlyMessage: '最小长度必须大于2'
    }],

    type: [{
        type: 'presence', message: '必须填写选择菜单类型'
    }],

    target: [{
        type: 'presence', message: '必须填写菜单目标'
    }, {
        type: 'length', min: 2, minOnlyMessage: '最小长度必须大于2'
    }],

    permission: [{
        type: 'presence', message: '必须填写菜单权限'
    }, {
        type: 'length', min: 2, minOnlyMessage: '最小长度必须大于2'
    }]
}

});
二、上面的代码是我配置的一个基类,其他model均从这个基类中继承而来。如果在基类中配置了schema中的namespace,那么子类的entityName就会是Model中去掉namespace的那部分。比如:’Ims.model.sys.Menu’这个类,因为它的基类有namespace:Ims.model,所以他的entityName默认自动产生就是sys.Menu。但是这样配置的话显然无法向后台请求,格式应该是:sys/menu这样的形式,所以我们也可以在子类Menu中手动配置entityName:sys/menu。
以上内容来源于官方文档:
Relative Naming

When describing associations between entities, it is desirable to use shorthand names that do not contain the common namespace portion. This is called the entityName as opposed to its class name. By default, the entityName is the full class name. However, if a namespace is used, the common portion can be discarded and we can derive a shorter name. In the following code, “MyApp.model.Foo” has an entityName of “Foo” and the schema has a namespace of “MyApp.model”.

If you use deeper nesting for entities, you may need to set the namespace config to account for this. For example:

Sample Code
Ext.define(‘MyApp.model.Base’, {
extend: ‘Ext.data.Model’,

 schema: {
     namespace: 'MyApp.model'
 }

});
Your derived classes now will generate proper default entityName values even if they have further namespaces. For example, “MyApp.model.foo.Thing” will produce “foo.Thing” as the entityName given the above as a base class.
三、配置shcema的另外一大好处是,基类proxy中API的地址可以动态生成,上面的基类中可以看到api的配置为: read: ‘{prefix}/{entityName:lowercase}/get’ ,prefix的值是urlPrefix,entityName可以自动生成也可以手动配置。
四、还有一个用处。如果想在每一个请求中都添加参数,可以在proxy中配置。
以上内容来源: http://www.extjstips.com/2015/10/13/extjs-using-a-base-model-and-schema/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值