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

Javascript Extjs 5,数据模型关联&;加载嵌套数据,javascript,extjs,extjs5,Javascript,Extjs,Extjs5,试着让这一切顺利 我想在两个对象模型上加载嵌套数据 Ext.application({ name : 'MyApp', launch : function() { Ext.define('MyApp.model.Address', { extend: 'Ext.data.Model', entityName: 'Address', fields: [ { name: 'i

试着让这一切顺利

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

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
尝试在模型定义上定义类似代理的内存或本地存储,但结果是一样的

分机小提琴:


任何帮助都将不胜感激

我一直在玩弄你手中的代码,到目前为止还不能让协会以正式方式运作

我使用以下代码模拟了功能:

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);
        }
    });
基本上,我已经删除了关联并创建了一个自定义函数来根据传入的原始数据创建一个模型记录,如果地址数据不存在,您还可以返回一个新的空模型,而不是null,我使用null是因为更容易确定您是否有有效的地址记录


如前所述-这不是正式的解决方法,我将再次尝试小提琴,一旦找到更好的解决方案,这可能会有所帮助。

解决,但只找到这个解决方案:使用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());
新提琴上的样品:


你说得对,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());

是的,非常感谢,这个解决方案对我来说很有效,但我想解释一下为什么官方的方式不起作用。老实说,我还在调查ExtJs协会有点古怪,或者至少很难习惯,只要看看所有关于它们的帖子就知道了。当我有更多信息时,我会更新答案。它似乎不喜欢您在这两个上都使用
id:
的事实,我将地址一更改为
addressId
,它通过了错误,但仍然没有加载数据。已解决,但仅当我使用
loadRawData
时。。。你可以看到这把新小提琴:。。。你是对的,ext有点不稳定,非常…它似乎不起作用,这段代码没有生成任何关联。。。使用“user.data.address”,您正在访问未解析的原始对象。查找user.data.address.created(它没有解析像user.get('created')…这样的日期对象…没有生成关联(您无法访问vía user.getAddress().get('created'))。感谢您的尝试。