ExtJS 6继承了hasMany-Relation嵌套加载

问题描述:

我尝试设置hasMany关系,并希望在单个请求中加载具有所有关联模型的主实体。 但这似乎不适用于继承的“hasMany”关系。 我有一个BaseModel,它定义了所有的关系和字段,以及一个定义要从中加载代理的“普通”模型。ExtJS 6继承了hasMany-Relation嵌套加载

这些是我的(相关)模式:

Ext.define('MyApp.model.BaseUser', { 
    "extend": "Ext.data.Model", 
    "uses": [ 
     "MyApp.model.UserEmail", 
     "MyApp.model.Account" 
    ], 
    "fields": [ 
     { 
      "name": "name" 
     }, 
     { 
      "name": "accountId", 
      "reference": { 
       "type": "MyApp.model.Account", 
       "role": "account", 
       "getterName": "getAccount", 
       "setterName": "setAccount", 
       "unique": true 
      } 
     } 
    ] 
    "hasMany": [ 
     { 
      "name": "emails", 
      "model": "MyApp.model.UserEmail" 
     } 
    ], 
}); 

Ext.define('MyApp.model.User', { 
    extend: "MyApp.model.BaseUser", 
    proxy: { 
     type: 'rest', 
     url : '/api/user', 
     reader: { 
      type: 'json', 
      rootProperty: 'data', 
     } 
    } 
}); 

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

    "fields": [ 
     { 
      "name": "id", 
      "type": "int" 
     }, 
     { 
      "name": "email", 
      "type": "string" 
     }, 
    ], 

    proxy: { 
     type: 'rest', 
     url : '/api/user/email', 
     reader: { 
      type: 'json', 
      rootProperty: 'data', 
     } 
    } 
}); 

// MyApp.model.Account looks like MyApp.model.UserEmail 

这是我的服务器的响应:

{ 
    data: [ 
     { 
      name: 'User Foo' 
      accountId: 50 
      account: { 
       id: 50, 
       balance: 0 
      }, 
      emails: [ 
       { 
        id: 70, 
        email: '[email protected]' 
       } 
      ] 
     }  
    ] 
} 

“账户”的关系正在“正常”的用户模型,我可以访问它通过自动生成的方法user.getAccount(),如我所料。

现在我试着用自动生成的方法来访问用户的电子邮件:

// user is of 'MyApp.model.User' 
user.emails(); // store 
user.emails().first(); // null 
user.emails().count(); // 0 

看来,“电子邮件” -relation模型没有加载到我的用户模型。我能以正确的方式访问它们吗? 我可以通过user.data.emails访问它们。但是这是一个普通对象数组,而不是UserEmail-Objects。

任何人都可以给我一些建议吗?无键协会支持嵌套加载吗?

亲切的问候, czed

编辑: 澄清的是我精神疾病。

它应该工作。这是一个工作fiddle。检查控制台嵌套加载的数据。

+0

谢谢你的回答。当我发现我的模型并不正确。我将这些关系定义为一个基本模型并将其扩展。这似乎是问题所在。我调整了我的帖子并将其清除。对不起 –