title: Cannot access associeted model in ExtJS
body: <p>I have two models in my ExtJS (4.2.1) application and would like to define a "hasOne" asscociation between the two models. However, I can't make it work.</p>
  
  <p>I tryed to respect the rules for the "hasOne" association as indicated in <a href="http://extjs-tutorials.blogspot.ch/2012/05/extjs-belongsto-association-rules.html" rel="nofollow noreferrer">http://extjs-tutorials.blogspot.ch/2012/05/extjs-belongsto-association-rules.html</a>.</p>
  
  <p><strong>Main Model</strong></p>
  
  <pre><code>Ext.define('Aap.model.TreeNode', {
  extend: 'Ext.data.Model',
  fields: [
      { name: 'name', type: "string"},
      { name: 'id'},
      { name: 'allgemein_id'}
  ],
  proxy: {
      type: 'ajax',
      url: 'data/treedata.json',
      reader: {
          type: 'json',
          root: 'nodes',
          successProperty: 'success'
      }
  },
  associations: [
      {
          type: 'hasOne',
          model: 'Aap.model.Allgemein',
          associationKey: 'metaallgemein',
          primaryKey: 'id',
          foreignKey: 'allgemein_id',
          getterName: 'getAllgemein',
          setterName: 'setAllgemein',
          autoLoad: true
      }
  ]
  </code></pre>
  
  <p>});</p>
  
  <p><strong>Associated model</strong></p>
  
  <pre><code>Ext.define('Aap.model.Allgemein', {
  extend: 'Ext.data.Model',
  requires: 'Aap.model.TreeNode',
  fields: [
      {name: "name", type: 'string'}, 
      {name: "allgemein_name", type: 'string'}, 
      {name: "ident", type: 'string'}, 
      {name: "georefdat", type: 'string'},
      {name: "fachst", type: 'string'}, 
      {name: "zugberech", type: 'string'}, 
      {name: "echkateg", type: 'string'}, 
      {name: "nachfzeitr", type: 'string'}, 
      {name: "datenmenge"}, 
      {name: "imjr"}, 
      {name: "datenzuw"}, 
      {name: "bemerk", type: 'string'},
      {name: "treenode_id", type: 'int'},
      {name: "id", type: "int"}
  ],
  associations: [
      {
          type: 'belongsTo',
          model: 'Aap.model.TreeNode'
      }
  ],
  proxy: {
      type: 'ajax',
      api: {
          read: 'data/allgemeindata.json'
      },  
      reader: {
          type: 'json',
          root: 'metaallgemein',
          successProperty: 'success'
      }
  }
  </code></pre>
  
  <p>});</p>
  
  <p><strong>Data for main model</strong></p>
  
  <pre><code>{
  "success": true,
  "nodes": [
      { "name": "st", "id":"1", "allgemein_id": "1", "expanded": false, "children": [
          { "name": "GeodÃ¤tische Grundlagen", "id":"4", "allgemein_id": "4", "children": [
              { "name": "Beispiel Datensatz", "id":"6", "allgemein_id": "6", "leaf": true }
          ]},
          { "name": "Bilddaten", "id":"5", "allgemein_id": "5", "loaded": true}
      ]},
      { "name": "BU", "id":"2", "allgemein_id": "2", "loaded": true },
      { "name": "BE", "id":"3", "allgemein_id": "3", "loaded": true }
  ]
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Data of associated model</strong></p>
  
  <pre><code>{
  "success": true,
  "metaallgemein": [
      { 
          "name":"st", 
          "ident": "stident", 
          "georefdat":"stgeorefdat",
          "fachst":"stfach", 
          "zugberech":"stzugangsbe", 
          "echkateg":"stechkateg", 
          "nachfzeitr":"stzeitrnachf", 
          "datenmenge":"stdatenmende", 
          "imjr":"stimjr", 
          "datenzuw":"stdatenzuw", 
          "bemerk":"stbemerk",
          "id": "1",
          "treenode_id": "ext-record-1"
      },
      { 
          "name":"BU", 
          "ident": "buident", 
          "georefdat":"bugeorefdat",
          "fachst":"bufach", 
          "zugberech":"buzugangsbe", 
          "echkateg":"buechkateg", 
          "nachfzeitr":"buzeitrnachf", 
          "datenmenge":"budatenmende", 
          "imjr":"buimjr", 
          "datenzuw":"budatenzuw", 
          "bemerk":"bubemerk",
          "id": "2",
          "treenode_id": "ext-record-2"
      }, ...
              ...
              ...
  </code></pre>
  
  <p>When I load my page and try to get the associated data with my getter, the following happens (see image).</p>
  
  <ul>
  <li>The first time I try to get the associated data (rec.getAllgemein()), I don not get
  any data.</li>
  <li>When I use the getter a second time (rec.getAllgemein()), I get data, but not the
  correct ones. I added a <a href="https://docs.google.com/file/d/0Byf7o8qnrNkBc0FpSXZTNmlFTEU/edit?usp=sharing" rel="nofollow noreferrer">Screenshot</a> helping to explain the problem. 
  It seem to be a more fundametal problem. Some data are associated, but not correctly. Other record do not have any association. </li>
  </ul>
  
  <p>I tried a simpler example in order to trace the problem. But I did not even achieve to make the association work in the most basic example. It seems that I make somewhere a fundametal mistake:</p>
  
  <pre><code>Ext.define('Aap.model.Person', {
  extend: 'Ext.data.Model',
  requires: 'Aap.model.Address',
  fields: [
      {name: 'id', type: 'int'},
      {name: 'name', type: 'string'}, 
      {name: 'address_id', type: 'int'}
  ],
  proxy: {
      type: 'ajax',
      api: {
        read: 'data/persons.json'
      },
      reader: { 
          type: 'json',
          root: 'children',
          successProperty: 'success'
      }
  },  
  associations: [
      { 
          type: 'hasOne', 
          model: 'Aap.model.Address'
      }
  ]
  </code></pre>
  
  <p>});</p>
  
  <pre><code>Ext.define('Aap.model.Address', {
  extend: 'Ext.data.Model',
  fields: [
      { name: 'id', type: 'int'},
      { name: 'city', type: 'string'}
  ],
  proxy: {
      type: 'ajax',
      api: {
        read: 'data/addresses.json'
      },
      reader: { 
          type: 'json',
          root: 'children',
          successProperty: 'success'
      }
  }   
  </code></pre>
  
  <p>});</p>
  
  <pre><code>Ext.define('Aap.store.Persons', {
  extend: 'Ext.data.Store',
  model: 'Aap.model.Person',
  autoLoad: 'true',
  storeId: 'persons'
  </code></pre>
  
  <p>});</p>
  
  <pre><code>Ext.define('Aap.store.Addresses', {
  extend: 'Ext.data.Store',
  model: 'Aap.model.Address',
  autoLoad: 'true',
  storeId: 'addresses'
  </code></pre>
  
  <p>});</p>
  
  <pre><code>{
  "success": true,
  "children": [
      { 
          "id": 1,
          "name": "Bernt",
          "address_id": 1
      },
      { 
          "id": 2,
          "name": "Ludowid",
          "address_id": 2
      }
  ]
  </code></pre>
  
  <p>}</p>
  
  <pre><code>{
  "success": true,
  "children": [
      { 
          "id": 1,
          "city": "Berlin"
      },
      { 
          "id": 2,
          "city": "London"
      }
  ]
  </code></pre>
  
  <p>}</p>
  
  <p>I don not know where the problem lies.</p>
  
  <ul>
  <li>Is it the model that is not correctly defined?</li>
  <li>Is there a problem with the data</li>
  <li>Is it the model that is not correctly defined?</li>
  <li>Or is it something completely different? </li>
  </ul>
  
  <p>Blog entries such as:
  <a href="https://stackoverflow.com/questions/6622878/extjs-4-models-with-associations-and-stores">ExtJS 4: Models with Associations and Stores</a>
  gave me a better understanding of how associations work. But they did'nt help me to solve the problem. </p>
  
  <p>Thanks for help!</p>