Extjs 5,数据模型关联&加载嵌套数据

Extjs 5, data model association & load nested data

正在尝试完成这项工作....

我想在两个对象模型上加载嵌套数据

Ext.application({
name : 'MyApp',

launch : function() {


    Ext.define('MyApp.model.Address', {
        extend: 'Ext.data.Model',

        entityName: 'Address',

        fields: [ 

            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'addressLine',
                type: 'string'
            },
            {
                name: 'city',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ]
    });


    Ext.define('MyApp.model.User', {
        extend: 'Ext.data.Model',

        entityName: 'User',

        fields: [ 
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'address',
                reference: 'Address'
            },
            {
                name: 'name',
                type: 'string'
            },
            {
                name: 'lastname',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ]
    });


    var user = new MyApp.model.User({
        "id": 1,
        "name": "Pedro",
        "lastname": "Carbonell",
        "address": {
            "id": 1,
            "addressLine": "Bailen 22",
            "city": "Barcelona",
            "created": 1420668866000
        },
        "created": 1420668866000
    });        

    console.info(user);
    console.info(user.getAddress());

}});

创建用户时没有错误,但是当我通过 user.getAddress() 访问关联数据时返回异常:

 Uncaught Error: The model ID configured in data ("[object Object]") has been rejected by the int field converter for the id fieldext-all-debug.js

尝试在模型定义中定义像 memory 或 localstorage 这样的代理,但结果是一样的。

分机 fiddle:https://fiddle.sencha.com/#fiddle/h2d

任何帮助将不胜感激!

我一直在研究您 fiddle 中的代码,但到目前为止还无法让协会以官方方式运作。

我使用这段代码模拟了功能:

Ext.define('MyApp.model.User', {
        extend: 'Ext.data.Model',

        fields: [{
            name: 'id',
            type: 'int'
        }, {
            name: 'name',
            type: 'string'
        }, {
            name: 'lastname',
            type: 'string'
        }, {
            name: 'created',
            type: 'date',
            dateFormat: 'time',
            persist: false
        }],

        getAddress: function() {
            if ('undefined' === this.data.address) {
                return null;
            }
            return Ext.create('Address', this.data.address);
        }
    });

基本上我已经删除了关联并创建了一个自定义函数来根据传入的原始数据创建模型记录,如果地址数据没有,您也可以 return 一个新的空模型exist 而不是 null,我使用 null 因为它更容易确定您是否有有效的地址记录。

如前所述-这不是执行此操作的官方方法,一旦找到更好的解决方案,我将再次尝试 fiddle 和 post,这可能会有所帮助与此同时。

已解决,但仅找到此解决方案:使用 loadRawData 时...

    var store = new Ext.data.Store({
        model: MyApp.model.User
    });

    store.loadRawData({
        "id": 1,
        "name": "Pedro",
        "lastname": "Carbonell",
        "address": {
            "id": 1,
            "addressLine": "Bailen 22",
            "city": "Barcelona",
            "created": 1420668866000
        },
        "created": 1420668866000
    });        

    console.info(store.first());
    console.info(store.first().getAddress());

在这个新的 fiddle 上采样:https://fiddle.sencha.com/#fiddle/h4e

你说得对,ext 有点不稳定,非常....

使用原始代码,我做了一些修改,现在它似乎可以工作了。

Ext.application({
name : 'MyApp',

launch : function() {


    Ext.define('MyApp.model.Address', {
        extend: 'Ext.data.Model',

        //entityName: 'Address',

        fields: [ 

            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'addressLine',
                type: 'string'
            },
            {
                name: 'city',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ],

        hasMany: 'User'
    });


    Ext.define('MyApp.model.User', {
        extend: 'Ext.data.Model',

        //entityName: 'User',

        fields: [ 
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'name',
                type: 'string'
            },
            {
                name: 'lastname',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ], 

        hasMany: { model: 'Address', name: 'Address' }
    });


    var user = new MyApp.model.User({
        "id": 1,
        "name": "Pedro",
        "lastname": "Carbonell",
        "address": {
            "id": 1,
            "addressLine": "Bailen 22",
            "city": "Barcelona",
            "created": 1420668866000
        },
        "created": 1420668866000
    });        

    console.info(user);
    console.info(user.data.address);

}
});

这就是您想要的吗?我在用户模型上手动设置地址。不理想,但它被正确地解释为记录。

    Ext.define('MyApp.model.Address', {
        extend: 'Ext.data.Model',

        entityName: 'Address',

        fields: [ 

            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'addressLine',
                type: 'string'
            },
            {
                name: 'city',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ]
    });


    Ext.define('MyApp.model.User', {
        extend: 'Ext.data.Model',

        entityName: 'User',

        fields: [ 
            {
                name: 'id',
                type: 'int'
            },
            {
                name: 'addressId',
                reference: 'Address'
            },
            {
                name: 'name',
                type: 'string'
            },
            {
                name: 'lastname',
                type: 'string'
            },
            {
                name: 'created',
                type: 'date',
                dateFormat: 'time',
                persist: false
            }
        ]
    });


    var user = new MyApp.model.User({
        "id": 1,
        "name": "Pedro",
        "lastname": "Carbonell",
        "created": 1420668866000
    });        

    var addr  = new MyApp.model.Address({
            "id": 1,
            "addressLine": "Bailen 22",
            "city": "Barcelona",
            "created": 1420668866000
    });
    user.setAddress(addr);
    console.info(user);
    console.info(user.getAddress());