Add a auto initialize function inside a Sencha Touch model to manipulate model's data

StackOverflow https://stackoverflow.com/questions/23360825

  •  11-07-2023
  •  | 
  •  

Question

When I load a store, due to the API structure, I get a json object inside my model:

Ext.define('TestApp.model.SecretKeyModel', {
    extend: 'Ext.data.Model',
    config:{
        identifier: {
            type: 'uuid'
        },
        fields: [
            { name:'theRecord', type:'json' }
        ]
    },
    extractToken: function() {

        var record = this;

        var token = record.initSession.token;
        console.log('token: ' + token);
    }
});

I need to extract the token from that json object.

For that, i think I should write a function right there, inside the model.

How can I call it, when the store loads, to manipulate the data and extract the token?

Was it helpful?

Solution

I faced a similar situation 1 hour ago, i needed to edit data during loading or immidiatly after. I ended with this solution:

Ext.define('TestApp.store.MyStore', {
    extend: 'Ext.data.Store',
    requires: ['TestApp.model.SecretKeyModel'],
    config :{
        model: 'TestApp.model.SecretKeyModel',
        proxy: {
            type: 'ajax',
            url: TestApp.utils.GlobalVariables.getServerUrl(),
            reader: {
                type: 'json',
                rootProperty: ''
            }
        },
        listeners: {
            refresh: function(store, data, eOpts) {
                store.each(function (item, index, length) {
                    item.extractToken(); // call this on every record loaded in the store
                });
            }
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top