Json Sencha2代理检索数据,但列表不显示

Json Sencha2代理检索数据,但列表不显示,json,list,proxy,sencha-touch-2,Json,List,Proxy,Sencha Touch 2,这就是正在发生的事情,我有一个列表,如果我使用存储上的data属性硬编码数据,我可以很好地工作,但是当我尝试使用代理时,没有显示任何内容。。。代理成功发送数据并从服务器接收数据,但列表仍然拒绝显示任何内容 这是一种观点: Ext.define('MyApp.view.myListView', { extend: 'Ext.List', xtype: 'myListView', requires: ['MyApp.store.myListStore'], config: { title:

这就是正在发生的事情,我有一个列表,如果我使用存储上的data属性硬编码数据,我可以很好地工作,但是当我尝试使用代理时,没有显示任何内容。。。代理成功发送数据并从服务器接收数据,但列表仍然拒绝显示任何内容

这是一种观点:

Ext.define('MyApp.view.myListView', { 
extend: 'Ext.List',
xtype: 'myListView',
requires: ['MyApp.store.myListStore'],
config: {
    title: 'American Companies',
    grouped: false,
    itemTpl: '{company} {contact}',
    store: 'myListStore',
    onItemDisclosure: true
}});
模型:

Ext.define('MyApp.model.myListModel', {  
    extend: 'Ext.data.Model',
    config: {
    fields: ['company', 'contact']
   }
});
商店:

Ext.define('MyApp.store.myListStore', {
    extend: 'Ext.data.Store',


    requires: ['MyApp.model.myListModel', 'MyApp.proxy.myListProxy'],


    config: {
        model: 'MyApp.model.myListModel',
        proxy: 'searchProxy',
        autoLoad: true,
        grouper: {
            groupFn: function (record) {
                return record.get('contact').substr(0, 1);
            }
        }
    }
}); 
代理人:

Ext.define('MyApp.proxy.myListProxy', {
    extend: 'Ext.data.proxy.Ajax',
    alias: 'proxy.searchProxy',
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    callbackKey: 'callback',
    type: 'json',
    config: {
        model: 'MyApp.model.myListModel',
        url:  '/myUrl that works',   
        reader: {
            type: 'json',
            rootProperty: 'data' // E.g. {results: [{id: 123, description: 'some     text'}, {...}]},
        }
    },
    read: function (operation, callback, scope) {
           Ext.Ajax.request({
            url: '/myUrl that works',   
            //success: passFn,   // function called on success
            failure: failFn,
            jsonData: {
                "data": {
                    "fields": ["company", "contact"],
                }
            }
        });

    },


    filterParam: undefined,


    /**
     * NOTE: so I can add other params as I needed the params for a search request
     * You could use extraParams instead
     */
    buildRequest: function(operation) {
        var request = this.callParent(arguments);
        var params  = request.getParams();
//        var searchRequest = getSearchRequest(); // helper method
//        if (searchRequest) {
//            Ext.apply(params, searchRequest);
//        }
        return request;
    }


});


function failFn(msg) {
    Ext.Msg.alert('Ajax Error', msg);
}

将您的
读取
覆盖更改为以下内容-

read: function (operation, callback, scope) {
    var that = this;

    Ext.Ajax.request({
        url: 'yout_url_here',
        success: function(response,request){
            var receivedData = Ext.JSON.decode(response.responseText.trim());

            operation.setResultSet(Ext.create('Ext.data.ResultSet', {
                records: receivedData.data,
                total  : receivedData.data.length
            }));

            operation.setSuccessful();
            operation.setCompleted();

            if (typeof callback == "function") {
                callback.call(scope || that, operation);
            }
        },
        failure: failFn,
        jsonData: {
            "data": {
                "fields": ["company", "contact"]
            }
        }
    });

},
按照读取方法的文件-

执行给定的读取操作。如果在自定义代理中重写此方法,请记住在完成操作时始终调用提供的回调方法

所以你需要再次调用回调。但仅仅打电话是不够的。从服务接收的数据需要分配到
Ext.data.Operation
。因此,新的
ResultSet
必须创建并为其分配数据。这将允许存储将数据分配给列表。如果未设置
ResultSet
,则存储区将不会加载数据

我用下面的json尝试了一下,它成功了-

{
   "data":[
      {
         "company":"a",
         "contact":"b"
      },
      {
         "company":"a",
         "contact":"b"
      },
      {
         "company":"a",
         "contact":"b"
      },
      {
         "company":"a",
         "contact":"b"
      }
   ]
}
试试看。但是,恕我直言,您可能不需要重写
read
方法。若要在将接收到的数据附加到列表之前对其进行处理,则可能需要此方法。我不知道你是否想要这个


但上述解决方案对我有效,对你也有效。:)

为什么要重写代理中的
read
方法?老实说,这就是它对我的工作方式。。。如果没有read函数,事实上如果没有ajax请求,我就做不到。。代理本身并没有发送任何数据,我对下面的答案很陌生。我自己试过了,效果很好DIt工作起来很有魅力!!谢谢!然而,我仍然对读者的问题感到困惑,你能告诉我如果没有读者会怎样吗?很高兴这对你有帮助。如果您只是删除
read
override,它应该可以工作。您已经在配置中为rootProperty和数据类型指定了默认值。这就足够了。我会尽量在业余时间提供细节谢谢!!我真的很想重构它,学习正确的方法,你已经做了很多,再次感谢!