Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait until all stores are Sync in ExtJs?

I have a list of grids that can change their data in form by end-user. Finally, I want to sync all the grids by clicking the button, then an operation is performed.

I wrote the code below:

$.when.apply(
    Ext.ComponentQuery.query('grid')
       .forEach(function(item) {
             if (item.getXType() == "grid") {
                if (item.store.getNewRecords().length > 0 || item.store.getUpdatedRecords().length > 0 || item.store.getRemovedRecords().length > 0) {
                    item.store.sync();
                 }
             }
})).then(function (results) {
    //do something
});

Problem is here that store.sync() not waiting for callback.

What is the recommended way of doing this?

like image 220
Hossein Avatar asked Sep 15 '18 10:09

Hossein


People also ask

What is proxy in ExtJS?

In ExtJs have store proxy and also Ajax request you can use both. Proxies are used by Ext. data. Store to handle the loading and saving of Ext. data.

What is the benefit of using Itemid?

The itemid global attribute provides microdata in the form of a unique, global identifier of an item. An itemid attribute can only be specified for an element that has both itemscope and itemtype attributes.

How do I create a grid in ExtJS?

create('Ext. grid. Panel', { store: { fields: ['name', 'email', 'phone'], sorters: ['name', 'phone'] }, columns: [ { text: 'Name', dataIndex: 'name' }, { text: 'Email', dataIndex: 'email' } ] }); Sorting at run time is easily accomplished by simply clicking each column header.

What is requires ExtJS?

You can look at requires as a way to tell ExtJS: "When you construct an object of this class, please make sure to dynamically load the required scripts first".


1 Answers

I do it with Promise like this:

 // Sync grid data if exist dirty data
 Promise.all(
     Ext.ComponentQuery.query('grid')
     .map(grid => grid.getStore())
     .filter(s => (s.getNewRecords().length + s.getUpdatedRecords().length + s.getRemovedRecords().length) > 0)
     .map(s => new Promise((resolve, reject) => {
           s.sync({
               success: () => { resolve(); },
               failure: () => { reject(); }
           });
      }))
      ).then(() => {
           //do something
      });
like image 165
Hossein Avatar answered Sep 24 '22 03:09

Hossein