V
V
Vlad2019-07-29 09:58:45
ExtJS/Sencha
Vlad, 2019-07-29 09:58:45

How to access the store created in the controller?

I study examples on ExtJs 6 and try to repeat. Here is an example of changing remote storage to local https://habr.com/ru/post/138054/ and I'm trying to repeat it.
The difficulty is caused by these fragments

UsersApp.Utils.ping({
    success: this._onPingSuccess, // Интернет есть
    failure: this._onPingFailure     // Интернета нет
  }, this);

where
_onPingSuccess: function(){
        // сеть есть
        var win           = Ext.ComponentQuery.query('#usersWindow')[0];
        var storeLocal = this.getStore('storeLocal');
        var store         = this.getStore('store');
        var grid           = win.getComponent('NamesGrid');

UsersApp.Utils.ping is a wrapper for Ext.Ajax.request, but the author does not disclose the code. When _onPingSuccess or _onPingFailure is executed, this refers to the Window object, which does not have a getStore method.
Full controller code
Ext.define("Apple.controller.Main", {
    extend: 'Ext.app.Controller',
    requires: [
      'Apple.utils.Ping',
      'Apple.store.OrderStore',
      'Apple.store.UserStore'
    ],

    init: function(){

      Ext.define("Session", {
        extend: "Ext.data.Session",
      });

      var session = Ext.create("Session");

      var store = Ext.create("Apple.store.OrderStore", {
        storeId: 'OrderStore',
        session: session
      });

      store.setProxy(
        Ext.create('Ext.data.RestProxy', {
          type: 'rest',
          url: 'https://localhost:5001/api/order',
          api: {
            create:  'https://localhost:5001/api/order',
            read:    'https://localhost:5001/api/order',
            update:  'https://localhost:5001/api/order',
            destroy: 'https://localhost:5001/api/order'
          },
          writer: {
            type: 'json',
            writeAllFields : false, //just send changed fields
            allowSingle : true      //always wrap in an array
          },
          reader: {
            type: 'json',
            rootProperty: 'data',
            successProperty: 'success'
          }
        })
      );

      var local = Ext.create("Apple.store.OrderStore", {
        storeId: 'OrderStoreLocal',
        session: session
      });

      local.setProxy(
        Ext.create('Ext.data.proxy.LocalStorage', {
          type: 'localstorage',
          id  : 'Orders'
        })
      );

      local.addListener('load', function(){
        Apple.utils.Ping.sendPing({
          success: this._onPingSuccess, 
          failure: this._onPingFailure
        }, this);
      }, this);
      
      // инициируем загрузку локальное хранилище
      local.load();
    },

    _onPingSuccess: function(response, options){

      var store = this.getStore('OrderStore');
      var local = this.getStore('OrderStoreLocal');

      var grid  = Ext.getCmp('LayC0Grid');

      // выясняем количество записей в локальном хранилище
      localCnt = local.getCount();
      
      // проверяем состояние локального хранилища,
      // выясняя, необходима ли синхронизация
      if (localCnt > 0){
        // синхронизация нужна, добавляем записи
        // по одной из локального хранилища
        // в серверное
        for (i = 0; i < localCnt; i++){
          var localRecord = local.getAt(i);
          var deletedId   = localRecord.data.id;
          delete localRecord.data.id;
          store.add(localRecord.data);
          localRecord.data.id = deletedId;
        }
        // сохраняем серверное хранилище
        store.sync();
        // очищаем локальное хранилище
        for (i = 0; i < localCnt; i++){
          local.removeAt(0);
        }
      }
        
      store.load();
      // подключаем к таблице серверное хранилище
      grid.reconfigure(store);
      grid.store.autoSync = true;
    },
    _onPingFailure: function(response, options){
      var local = this.getStore('OrderStoreLocal');
      var store = this.getStore('OrderStore');

      var grid  = Ext.getCmp('LayC0Grid');

      // устанавливаем хранилище таблицы на локальное
      grid.reconfigure(storeLocal);
      grid.store.autoSync = true;
    }
  });

How can I get storage in _onPingSuccess and _onPingFailure without passing them explicitly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question