пятница, 20 ноября 2015 г.

Много-к-одному без ключей

 Если загружаются вложенные модели без указания внешних ключей, то следует делать так.

https://www.sencha.com/forum/showthread.php?290275-How-to-achieve-the-equivalent-of-hasMany-using-reference&p=1061636&viewfull=1#post1061636

{
    "data": {
        "id":58,
        "login":"someLogin",
        "password":null,
        "name":"Some Name",
        "isActive":true,
        "createdAt":"2015-10-22 23:59:59",
        "roles":[
            {"id":1,"name":"admin","isActive":true,"createdAt":"2015-09-09 23:59:59"},
            {"id":3,"name":"operator","isActive":false,"createdAt":"2014-11-22 23:59:59"}
        ]
    }
}
 
 
Ext.define('Ui.model.admin.User', {
    extend: 'Ui.model.Base',
    requires: [
        'Ui.model.Proxy'
    ],
    fields: [
        {
            name: 'id', 
            type: 'int'
        },
        {
            name: 'login',
            type: 'string',
            allowNull: true
        },
        {
            name: 'password',
            type: 'string',
            allowNull: true
        },
        {
            name: 'name',
            type: 'string',
            allowNull: true
        },
        {
            name: 'createdAt',
            type: 'date',
            dateFormat: 'Y-m-d H:i:s',
            allowNull: true
        },
        {
            name: 'isActive',
            type: 'boolean',
            allowNull: true
        }
    ],
    hasMany: [{
        name: 'roles',
        model: 'Ui.model.admin.Role',
        associationKey: 'Roles'
    }],

    proxy: {
        type: 'uiModelProxy',
        url: '/users'
    }
});

Ext.define('Ui.model.admin.Role', {
    extend: 'Ui.model.Base',
    requires: [
        'Ui.model.Proxy'
    ],
    fields: [
        {  
            name: 'id', 
            type: 'int'
        },
        {  
            name: 'name',
            type: 'string'
        },
        {
            name: 'createdAt',
            type: 'date',
            dateFormat: 'Y-m-d H:i:s',
            allowNull: true
        },
        {
            name: 'isActive',
            type: 'boolean',
            allowNull: true
        }
    ],
    proxy: {
        type: 'uiModelProxy',
        url: '/roles'
    }
});


Либо в дочерней модели определить ссылочное поле, как в одной из предыдущих статей.

1 комментарий:

  1. Casino City - JTG Hub
    Visit Casino City. This 오산 출장안마 5-star destination 천안 출장샵 is minutes from Harrahs Reno and 남원 출장안마 minutes from Harrahs Reno. CasinoCity.com boasts over 양주 출장샵 100 통영 출장마사지 gaming

    ОтветитьУдалить