title: Understanding how stores models are referenced/loade/instantiated in Extjs 4.2 MVC pattern
body: <p>Using Extjs 4.2</p>
  <p>I have read many docs,google,forums trying to understand how components load and where to place them such as stores, models, ect but still confused.</p>
  <p>This is an example I am trying to get working</p>
  <p>Application description</p>
  <p>Main menu with Contacts,Projects,People etc etc, app first loads to display a static non data-driven, then use clicks on Contacts which displays a grid with list of contacts.  User then clicks on a contact row and popup edit view is displayed.</p>
  <p>In contacteditview the contact is loaded into the form in addition the form has a combobox that loads ContactTypes store.  The ContactType should be set to contacttype value for that contact record.</p>
  <p>What is the common way to do this considering this is a large application and I only want to load the data when it is need ie the view is displayed.</p>
  <p>Here are some of my confusions</p>
  <ol>
  <li><p>You can define all of you Controllers, Stores, Models, Views in the app config, but then everything is loaded no matter what page you are looking at.  In addition if you have autoLoad: true on your stores then calls for all the stores are made to the database even though you are not looking at that particular view.</p>
  </li>
  <li><p>When you specify a store or model in the stores or models property in your controller, what exactly does that do?  Is it just letting you easily reference the store but not actually creating it or does it create it or is it just setting getter and setter functions for convenience.  Example if I specify a store in the store property of my controller and have autoLoad true all data is loaded with no need to do anything else.  But what I really want is for that store to only load when I click on Contact and the list view is displayed.  So I set autoLoad:false and in my list function I manually get store using <code>this.getStore('Contacts')</code>. This works fine, but what is purpose of using stores and models array property of controller.  I see in debugger that if I do not use the store/model properties a get request is made for those js files.</p>
  <p>Sorry I know this is a lot but it is very confusing to me and have been struggling with it for awhile.</p>
  </li>
  <li><p>Lastly, I have the Contact grid loading only when you click on the Contacts button by setting autoLoad:false and manually loading.  Now when a user clicks edit the record loads fine but how do I get the combobox to load and then select the correct value.  I think part of my problem is understanding how stores models get loaded and instantiated.  In my combobox store property I have specified ContactType as the store but I get error store undefined, so either the js file is not loaded or store is not instantiated.</p>
  </li>
  </ol>
  <hr />
  <p>Here is my code so far.</p>
  <blockquote>
  <p>App Code</p>
  </blockquote>
  <pre><code>Ext.Loader.setConfig({
      enabled: true,
      paths: {
          'Ext.ux': &quot;lib/extux&quot;,
          'Wakanda': &quot;lib/extux/wakanda&quot;
      }
  });
  Ext.application({
      name: 'SimplyFundraising',
      autoCreateViewport: true,
  
      requires: ['Ext.ux.Router', // Require the UX
          'Wakanda.model'
      ],
  
      controllers: ['Contacts'],
  });
  </code></pre>
  <hr />
  <blockquote>
  <p>Contacts Controller</p>
  </blockquote>
  <pre><code>Ext.define('SimplyFundraising.controller.Contacts', {
      extend: 'Ext.app.Controller',
  
  
      views: ['contacts.List', 'contacts.Edit'],
      init: function() {
          this.control({
              'contactslist': {
                  itemdblclick: this.editContact,
                  removeitem: this.removeContact
              },
              'contactslist &gt; toolbar &gt; button[action=create]': {
                  click: this.onCreateContact
              },
              // 'contactsadd button[action=save]': {
              // click: this.doCreateContact
              // },
              'contactsedit button[action=save]': {
                  click: this.updateContact
              }
          });
      },
      list: function() {
  
          var mystore = this.getStore('Contacts')
          mystore.load();
          // mystore.proxy.extraParams = { $expand: 'ContactType'};
          //        var User = this.getContactModel();
          //        User.load(258, {
          //            success: function (user) {
          //                console.log(&quot;Loaded user 258: &quot; + user.get('lastName'));
          //            }
          //        });
      },
      editContact: function(grid, record) {
          var view = Ext.widget('contactsedit');
          view.down('form').loadRecord(record);
          this.addnew = false
      },
      removeContact: function(Contact) {
          Ext.Msg.confirm('Remove Contact ' + Contact.data.lastName, 'Are you sure?', function(button) {
              if (button == 'yes') {
                  this.getContactsStore().remove(Contact);
              }
          }, this);
      },
      onCreateContact: function() {
          var view = Ext.widget('contactsedit');
          this.addnew = true
      },
      // doCreateContact: function (button) {
      // var win = button.up('window'),
      // form = win.down('form'),
      // values = form.getValues(),
      // store = this.getContactsStore();
      // if (form.getForm().isValid()) {
      // store.add(values);
      // win.close();
      // }
      // },
      updateContact: function(button) {
          var win = button.up('window'),
              form = win.down('form'),
              record = form.getRecord(),
              values = form.getValues(),
              store = this.getContactsStore();
          if (form.getForm().isValid()) {
              if (this.addnew == true) {
                  store.add(values);
              } else {
                  record.set(values);
              }
              win.close();
          }
      }
  });
  </code></pre>
  <hr />
  <blockquote>
  <p>Contacts view list</p>
  </blockquote>
  <pre><code>Ext.define('SimplyFundraising.view.contacts.List', {
      extend: 'Ext.grid.Panel',
      xtype: 'contactslist',
      title: 'All Contacts',
      store: 'Contacts',
      autoHeight: true,
      autoScroll: true,
      viewConfig: {
          loadMask: true
      },
      initComponent: function() {
          this.tbar = [{
              text: 'Create Contact',
              action: 'create'
          }];
          this.columns = [{
                  header: 'Id',
                  dataIndex: '__KEY',
                  width: 50
              }, {
                  header: 'First Name',
                  dataIndex: 'firstName',
                  flex: 1
              }, {
                  header: 'Middle Name',
                  dataIndex: 'middleName',
                  flex: 1
              }, {
                  header: 'Last Name',
                  dataIndex: 'lastName',
                  flex: 1
              },
              {
                  header: 'Type',
                  dataIndex: 'ContactType.name',
                  flex: 1
              }
          ];
          this.addEvents('removeitem');
          this.actions = {
              removeitem: Ext.create('Ext.Action', {
                  text: 'Remove Contact',
                  handler: function() {
                      this.fireEvent('removeitem', this.getSelected())
                  },
                  scope: this
              })
          };
          var contextMenu = Ext.create('Ext.menu.Menu', {
              items: [this.actions.removeitem]
          });
          this.on({
              itemcontextmenu: function(view, rec, node, index, e) {
                  e.stopEvent();
                  contextMenu.showAt(e.getXY());
                  return false;
              }
          });
          this.callParent(arguments);
      },
      getSelected: function() {
          var sm = this.getSelectionModel();
          var rs = sm.getSelection();
          if (rs.length) {
              return rs[0];
          }
          return null;
      }
  });
  </code></pre>
  <hr />
  <blockquote>
  <p>Contacts view edit</p>
  </blockquote>
  <pre><code>Ext.define('SimplyFundraising.view.contacts.Edit', {
      extend: 'Ext.window.Window',
      xtype: 'contactsedit',
      title: 'Edit Contacts',
      layout: 'fit',
      autoShow: true,
      initComponent: function() {
          this.items = [{
              xtype: 'form',
              bodyStyle: {
                  background: 'none',
                  padding: '10px',
                  border: '0'
              },
              items: [{
                  xtype: 'textfield',
                  name: 'firstName',
                  allowBlank: false,
                  fieldLabel: 'Name'
              }, {
                  xtype: 'textfield',
                  name: 'lastName',
                  allowBlank: false,
                  fieldLabel: 'Last Name'
              }, {
                  xtype: 'combobox',
                  fieldLabel: 'Contact Type',
                  name: 'contactType',
                  store: 'ContactTypes',
                  displayField: 'name',
                  typeAhead: true,
                  queryMode: 'local',
                  emptyText: 'Select a type...'
              }]
          }];
          this.buttons = [{
              text: 'Save',
              action: 'save'
          }, {
              text: 'Cancel',
              scope: this,
              handler: this.close
          }];
          this.callParent(arguments);
      }
  });
  </code></pre>
  <hr />