user1173116
user1173116

Reputation: 39

How to add response from AjaxRequest to a store or display it on a list

I had a sencha touch code which connects to a WebService using Ext.Ajax.request. On success function, I want the response to be displayed in a list or store it in a Store.

Please help me how to do that. Below is my code.

  var View = function() {
        Ext.getBody().mask('Loading...', 'x-mask-loading', false);
        Ext.Ajax.request({
            url: 'URL',
            method: 'GET',
            success: function(response, opts) 
            {
                    var obj = Ext.util.JSON.decode(response.responseText);
                    Ext.getCmp('content').update(response.responseText);
            }
        });
    };

Upvotes: 1

Views: 1176

Answers (1)

rwe
rwe

Reputation: 177

create a Ext.List with store attribute,

myStore = new Ext.data.Store({
        model: Ext.regModel('', {

        fields: [
            { name: 'id', type: 'int' },
            { name: 'name', type: 'string'},
                            { name: 'age', type: 'string'}

        ]

       })
    });

myList = new Ext.List({
        store: myStore,
        itemTpl: '<div>{name}</div>' +'<div>{age}</div>'

    });

Then fill the myStore store with results of Agax request. onsuccess event in ajax call should be as follows,

 var jsonData = Ext.util.JSON.decode(response.responseText);
 myStore.add(jsonData['myresultlist']);
 myStore.sync();

Then make sure You are returning a valid json from the server side as follows,

{
"myresultlist": [
    {
        "id": "1",
        "name": "anne",
        "age": "21"
    },
    {
        "id": "2",
        "name": "jack",
        "age": "26"
    },
    {
        "id": "3",
        "name": "Tom",
        "age": "21"
    }
],
"success": "true",
"info": "My Results List!"
}

Upvotes: 0

Related Questions