简体   繁体   English

Ext JS 6中的一对一的预紧关系

[英]One-to-one with preload relations in Ext JS 6

I'm trying to setup a working one-to-one relation in Ext JS 6 that can be preloaded with the data of the referenced object(s). 我正在尝试在Ext JS 6中设置一个可工作的一对一关系,该关系可以预加载所引用对象的数据。

This means that i should be able to define a class Foo and a class Bar that references Foo and then do b = Ext.create(Bar, data) in such a way that b.something contains a Foo object initialized with data. 这意味着我应该能够定义一个Foo类和一个引用Foo Bar类,然后以b.something包含一个用数据初始化的Foo对象的方式执行b = Ext.create(Bar, data)

Reading the doc here it seems quite simple, except that it does not work: 这里阅读文档似乎很简单,除了它不起作用:

data = {'address': {'address': 'foo', 'city': 'foo', 'state': 'foo'}}
u = Ext.create('User', data)
u.getAddress()
none

The issue is also discussed here but the proposed solution seems not to work in Ext JS 6. 这里也讨论了这个问题但是建议的解决方案似乎在Ext JS 6中不起作用。

Do someone have a working example in Ext JS 6 or a general solution? 有人在Ext JS 6或通用解决方案中有可行的示例吗?

PS to the overzealous moderator: this is NOT a duplicate of any similar question regarding Ext JS 4.* or Ext JS 5 because they have a different implementation of the relations therefore those solutions won't work. 致过度热情的主持人的PS:这不是有关Ext JS 4. *或Ext JS 5的任何类似问题的重复,因为它们对关系的实现方式不同,因此这些解决方案将不起作用。

Here's an example: 这是一个例子:

Ext.define('Fiddle.model.Base', {
    extend: 'Ext.data.Model',

    schema: {
        namespace: 'Fiddle.model'
    }
});

Ext.define('Fiddle.model.User', {
    extend: 'Fiddle.model.Base',

    fields: [{
        name: 'address',
        reference: 'Address'
    }],

    proxy: {
        type: 'ajax',
        idParam: null,
        url: 'user.json'
    }
});

Ext.define('Fiddle.model.Address', {
    extend: 'Fiddle.model.Base'
});


var user = Ext.create('Fiddle.model.User', {
    id: 1
});

user.load({
    callback: function(rec) {
        console.log(rec.getAddress().getData())
    }
});

https://fiddle.sencha.com/#fiddle/12up https://fiddle.sencha.com/#fiddle/12up

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM