简体   繁体   中英

How to perform action on the basis of response status while doing sync in Ext.js

This is my store MyStore.js

Ext.define('myProject.store.MyStore', {
    config:{
        storeId:    'MyStore',
        autoLoad:   false,
        autoSync:   false,
        allowSingle: true,
        clearOnPageLoad:    true,

        model:              'abc.model.MyStoreModel',
        proxy: {
            type:   'rest',
            actionMethods: {create: 'POST', read: 'GET', update: 'POST', destroy: 'POST'},
              url:'/services/rest/MyService/myService',
            reader: {
                type: 'json',
                rootProperty:MyServiceView.elements.collection',
                successProperty : 'success'
            },
            writer:
            {
                type: 'json',
                root: 'MyServiceDataView',
                nameProperty: 'mapping',
                expandData : true
            },
            listeners: {
                exception:          function(proxy,response,operation){

                }
            }   
        }
    }
});

This is my Model

Ext.define( 'myProject.model.MyStoreModel', {
    extend:         'Ext.data.Model',
    config:{
        idProperty:     'keyStr',
        fields:[
                {
                    name:           'keyStr',
                    type:           'string',
                },
                {
                    name:           'sId',
                    type:           'int'
                },
                {
                    name:           'dCode',
                    type:           'string'
                },
                {
                    name:           'sNumber',
                    type:           'int'
                }

            ]
    },

});

Inside my Controller.js, I have this method

syncMyStore: function()
    {

        var deferred = Q.defer();
        var successfulSync= 'false';
        var me = this;
        var myStore = Ext.getStore('MyStore');

        if(this.isSyncRequires(myStore)) //assume this is always true
        {
            myStore.sync({
                success: function () {
                    successfulSync = 'true';
                    deferred.fulfill();
                }
            });
        }
        else
        {
            deferred.fulfill();
        }
        return successfulSync;
    },
  1. Suppose I have 5 records in my store ie record0, record2 ... record4.
  2. For each records, it is calling the Rest Service. So total 5 Rest calls

Requirement 1: Instead of using success property, I want to perform some actions on the basis of status code. ie if status code is 200, then consider it success.

Requirement 2: After each rest call, I want to remove record/mark dirty as false on the basis of response status (200) for that particular record.

Means, suppose for record1 and record2 if status code is 200, then I want to remove/mark dirty=false for record 1 and record2 only.

I will be really thankful for you if you help me out with this.

Assuming that record0, record1, record3 .. have some unique way of identification. Make an ajax call for each record(REST api call ) then if the response is 200, control will hit "success" function. Then if the response has data to identify records select that record and change its dirty flag to false. In the failure function do the same and change dirty flag to true. There are two things you need to conform to make better answer

  1. Will you be using records as payload for your rest api call ?
  2. Will the response have indication record, ie i'll have guid related to record ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM