JavaScript ExtJS 5: Accessing bound store in component extjs get bound store

I'm having a real hard time understanding how to access the actual bound store instance on a component, like a ComboBox or Grid. It appears that when you use their getStore method, it returns a different instance.

In my example, I have a custom grid class, and in its initComponent, I want to add a load listener. Unfortunately, the load listener never gets hit, but the bound store's load listener does. You can see the listener is getting set up before the store is loaded, so that's not the issue. It looks like the store instance in initComponent is a memory store, so the bound store's instance is not synced to the component at this point.

I know I can have a load listener on the bound store, but this is a custom grid class, and for any custom grid class, I want there to be a listener set up... so I don't want to have to do this all over the place in my code... I just want it in one class, as they're all going to have the same logic carried out.

Can someone explain to me why my custom grid class's load listener is not getting hit, and what can I do to fix this? Can I somehow access the bound store instance?

Ext.application({
  name: 'Fiddle',

  launch: function() {
    Ext.define('MyGrid', {
      extend: 'Ext.grid.Panel',
      xtype: 'myGrid',
      initComponent: function() {
        alert('initComponent')
        this.callParent();
        this.getStore().on('load', this.onLoad, this);
      },
      onLoad: function() {
        alert('grid store loaded');
      }
    });
    Ext.define('MyViewController', {
      extend: 'Ext.app.ViewController',
      alias: 'controller.myview',
      onLoadBoundStore: function() {
        alert('loaded bound');
      }
    })
    Ext.define('MyViewModel', {
      extend: 'Ext.app.ViewModel',
      alias: 'viewmodel.myview',
      stores: {
        myStore: {
          fields: ['name', 'value'],
          autoLoad: true,
          proxy: {
            type: 'ajax',
            url: 'data1.json',
            reader: {
              type: 'json'
            }
          },
          listeners: {
            load: 'onLoadBoundStore'
          }
        }
      }
    });
    Ext.define('MyView', {
      extend: 'Ext.form.Panel',
      renderTo: Ext.getBody(),
      controller: 'myview',
      viewModel: {
        type: 'myview'
      },
      items: [{
        title: 'blah',
        xtype: 'myGrid',
        reference: 'myComboBox',
        bind: {
          store: '{myStore}'
        },
        columns: [{
          text: 'Name',
          dataIndex: 'name'
        }, {
          text: 'Value',
          dataIndex: 'value'
        }]
      }]
    });
    Ext.create('MyView')
  }
});
Answer:1

Can someone explain to me why my custom grid class's load listener is not getting hit, and what can I do to fix this?

You are not initially providing any store to your grid (the store config option is required by the way). Hence the grid is creating its own, empty store as coded here:

store = me.store = Ext.data.StoreManager.lookup(me.store || 'ext-empty-store');

That empty store is the one that gets your onLoad listener attached. It is not getting hit because the store is never loaded.

Can I somehow access the bound store instance?

Yes, use this:

this.lookupViewModel().getStore('myStore')

You can attach your listener to the bound store instead of the empty one and see the difference. The grid's empty store eventually gets replaced by the bound one (this is why you see data in the grid, after all), but this happens after initComponent is executed. You can track the moment of setting the bound store to the grid by overriding setStore method.

I know I can have a load listener on the bound store, but this is a custom grid class, and for any custom grid class, I want there to be a listener set up... so I don't want to have to do this all over the place in my code... I just want it in one class, as they're all going to have the same logic carried out.

Since you are using MVVC it is recommended to stick to the pattern and keep views declaring view aspects only. Listeners should be declared and handled in controllers (not even in viewmodels like in your example). Otherwise, if you continue sticking to pre Ext JS 5 style and put dynamic stuff in views (i.e. the grid in your case) there is no point in using MVVC.

Answer:2

This is so incredibly hacky that I don't even want to post it as an answer, but I'm going to just for posterity sake. Building on Drake's answer, I figured out how to get my store using what was passed in the initial config of my grid class. The bind comes in with the braces, so that's why I'm doing a replace on it. Like I said, this probably should never be used, but it's something.

initComponent: function() {
  var store = this.lookupViewModel().getStore(this.config.bind.store.replace(/[{}]/g, ''));
  store.on('load', this.onLoad, this);
  this.callParent();
},
Answer:3

I am trying to iterate through data on success in an AJAX call. The data, when logged in console looks like this: [Object, Object] 0: Object messages: Array[0] sid: 534 __proto__: Object 1: ...

I am trying to iterate through data on success in an AJAX call. The data, when logged in console looks like this: [Object, Object] 0: Object messages: Array[0] sid: 534 __proto__: Object 1: ...

  1. iterating through array javascript
  2. iterating through array python
  3. iterating through array java
  4. iterating through array c++
  5. iterating through array matlab
  6. iterating through array ruby
  7. iterating through array bash
  8. iterating through array js
  9. iterating through arraylist
  10. iterating through array php
  11. iterating through array swift
  12. iterating through array of objects javascript
  13. iterating through array powershell
  14. iterating through array in typescript
  15. iterating through array in scala
  16. iterating through array in perl

i'm reading the book 'Secrets of the JavaScript Ninja' (http://jsninja.com/) and wonder why a certain code block from one of the examples is coded like it is. The example (http://jsfiddle.net/...

i'm reading the book 'Secrets of the JavaScript Ninja' (http://jsninja.com/) and wonder why a certain code block from one of the examples is coded like it is. The example (http://jsfiddle.net/...

I am using the following plugin for a datepicker: http://xdsoft.net/jqplugins/datetimepicker/ This is a very nice plugin however I am finding it difficult to set a default date when loading the box. ...

I am using the following plugin for a datepicker: http://xdsoft.net/jqplugins/datetimepicker/ This is a very nice plugin however I am finding it difficult to set a default date when loading the box. ...

I want to perform a custom assertion about the argument passed to a spied-on function call. Can I supply a callback to be used within the expectation against an argument? expect(my.method)....

I want to perform a custom assertion about the argument passed to a spied-on function call. Can I supply a callback to be used within the expectation against an argument? expect(my.method)....