1

如何添加或保存从 ajax 请求获取的数据以存储或建模 sencha touch 2 我有控制器、存储和模型。Ext.Ajax.request(); 从控制器调用,当它成功时,我想将该数据以 json 格式存储

Ext.define('Myapp.controller.HomeController', {
    extend: 'Ext.app.Controller',

    config: {
        control: {

            "#homepage_id": {
                show: 'onHomepage_idShow'
            }
        }

    },


    onHomepage_idShow: function (component, eOpts) {

        var token = localStorage.getItem('Token'); //**************************************
        console.log('test home', token);

        var customHeaders = {
            'Content-Type': 'application/json; charset=utf-8',
            'ApiAuth': token
        };

        this.callAjax(customHeaders);
    },
    callAjax: function (headers) {
        var customHeaders = headers;

        Ext.Ajax.request({
            url: 'http://localhost:9098/Folder/json/Get',
            params: Ext.util.JSON.encode({
                folderId: 0
            }),
            method: 'POST',

            headers: customHeaders,

            success: function (response) {
                var decode_text = Ext.decode(response.responseText);
                /*I want to add decode_text to a store from this contoller..*/

                //var storez = Ext.data.StoreManager.lookup('commomStore_id');//****************

                //this.getDataList().setStore(storez);
                console.log(storez);


                // process server response here
            },
            failure: function (response, opts) {
                Ext.Msg.alert('Error', 'Error while submitting the form');
                console.log(response.responseText);
            },

            scope: this
        });

我的店铺:

Ext.define('Myapp.store.CommonStore', {
    extend: 'Ext.data.Store',

    requires: [
        'Myapp.model.AuthTokenmodel'],

    config: {
        autoLoad: true,
        model: 'Myapp.model.AuthTokenmodel',
        storeId: 'commonStote_id',
        proxy: {

            type: 'localstorage',
            id: 'commomStore_id',
            reader: {
                type: 'json'
            }
        },
        fields: [{
            name: 'authtoken'
        }]
    }
});
4

2 回答 2

0

为此,您必须解析您的响应并从中创建Myapp.model.AuthTokenmodel对象,然后使用add方法将这些对象添加到您的存储中。

顺便说一句,如果您的响应是 JSOn 格式,您应该将其解析为 JSON 而不是这样的文本:

var respObj = Ext.JSON.decode(response.responseText);
console.log(respObj);

然后使用数据创建模型对象respObj并将其添加到存储:

var storez = Ext.data.StoreManager.lookup('commomStore_id');
storez.add(Ext.create('Myapp.model.AuthTokenmodel', {authtoken : respObj.authToken}));
于 2013-05-09T12:06:34.040 回答
0
Ext.getStore('commomStore_id').loadData(decode_text);
于 2013-05-09T18:53:25.513 回答