2017-10-05 6 views
0

私はhasMany関係をセットアップしようとしており、関連するすべてのモデルを1つのリクエストでメインエンティティにロードします。 しかし、それは継承されている "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ではなく、単純なオブジェクトの配列です。

誰かから助言をいただけますか?ネストされたローディングはキーなしのアソシエーションでサポートされていますか? Clearifiedは私がメンター何:

敬具は、 は

編集をczed。

答えて

0

これはうまくいくはずです。ここには作業fiddleがあります。コンソールに入れ子になっているデータがないかチェックしてください。

+0

ありがとうございます。私のモデルが正しいとは限りませんでした。関係を基本モデルに定義して拡張しました。それは問題のようです。私は私の投稿に適応し、これを解決しました。そのために残念 –

関連する問題