【问题标题】:problem with JsonStore and JsonReaderJsonStore 和 JsonReader 的问题
【发布时间】:2011-03-01 13:31:06
【问题描述】:

在我的 ExtJS 应用程序中,我使用 EditorGridPanel 来显示来自服务器的数据。

var applicationsGrid = new Ext.grid.EditorGridPanel({
    region: 'west',
    layout: 'fit',
    title: '<img src="../../Content/img/app.png" />  Приложения',
    collapsible: true,
    margins: '0 0 5 5',
    split: true,
    width: '30%',
    listeners: { 'viewready': { fn: function() { applicationsGridStatusBar.setText('Приложений: ' + applicationsStore.getTotalCount()); } } },
    store: applicationsStore,
    loadMask: { msg: 'Загрузка...' },
    sm: new Ext.grid.RowSelectionModel({
        singleSelect: true,
        listeners: { 'rowselect': { fn: applicationsGrid_onRowSelect} }
    }),
    viewConfig: { forceFit: true },
    tbar: [{
        icon: '../../Content/img/add.gif',
        text: 'Добавить'
    }, '-', {
        icon: '../../Content/img/delete.gif',
        text: 'Удалить'
    }, '-'],
    bbar: applicationsGridStatusBar,
    columns: [{
        header: 'Приложения',
        dataIndex: 'ApplicationName',
        tooltip: 'Наименование приложения',
        sortable: true,
        editor: {
            xtype: 'textfield',
            allowBlank: false
        }
    }, {
        header: '<img src="../../Content/img/user.png" />',
        dataIndex: 'UsersCount',
        align: 'center',
        fixed: true,
        width: 50,
        tooltip: 'Количество пользователей приложения',
        sortable: true
    }, {
        header: '<img src="../../Content/img/role.gif" />',
        dataIndex: 'RolesCount',
        align: 'center',
        fixed: true,
        width: 50,
        tooltip: 'Количество ролей приложения',
        sortable: true}]
    });

当我在没有阅读器的情况下使用 JsonStore 时,它​​可以工作,但是当我尝试更新任何字段时,它使用我的“创建”网址而不是“更新”。

var applicationsStore = new Ext.data.JsonStore({
    root: 'applications',
    totalProperty: 'total',
    idProperty: 'ApplicationId',
    messageProperty: 'message',
    fields: [{ name: 'ApplicationId' }, { name: 'ApplicationName', allowBlank: false }, { name: 'UsersCount', allowBlank: false }, { name: 'RolesCount', allowBlank: false}],
    id: 'app1234',
    proxy: new Ext.data.HttpProxy({
        api: {
            create: '/api/applications/getapplicationslist1',
            read: '/api/applications/getapplicationslist',
            update: '/api/applications/getapplicationslist2',
            destroy: '/api/applications/getapplicationslist3'
        }
    }),        
    autoSave: true,
    autoLoad: true,
    writer: new Ext.data.JsonWriter({
        encode: false,
        listful: false,
        writeAllFields: false
    })
});

我认为问题在于我不使用阅读器,但是当我使用 JsonReader 时,网格根本停止显示任何数据。

var applicationReader = new Ext.data.JsonReader({
    root: 'applications',
    totalProperty: 'total',
    idProperty: 'ApplicationId',
    messageProperty: 'message',
    fields: [{ name: 'ApplicationId' }, { name: 'ApplicationName', allowBlank: false }, { name: 'UsersCount', allowBlank: false }, { name: 'RolesCount', allowBlank: false}]
});

var applicationsStore = new Ext.data.JsonStore({
    id: 'app1234',
    proxy: new Ext.data.HttpProxy({
        api: {
            create: '/api/applications/getapplicationslist1',
            read: '/api/applications/getapplicationslist',
            update: '/api/applications/getapplicationslist2',
            destroy: '/api/applications/getapplicationslist3'
        }
    }),
    reader: applicationReader,
    autoSave: true,
    autoLoad: true,
    writer: new Ext.data.JsonWriter({
        encode: false,
        listful: false,
        writeAllFields: false
    })
});

那么,有谁知道问题可能是什么以及如何解决它。从我的服务器返回的数据是 Json 格式的,并且接缝没问题

{"message":"test","total":2,"applications":[{"ApplicationId":"f82dc920-17e7-45b5-98ab-03416fdf52b2","ApplicationName":"Archivist","UsersCount":6,"RolesCount":3},{"ApplicationId":"054e2e78-e15f-4609-a9b2-81c04aa570c8","ApplicationName":"Test","UsersCount":1,"RolesCount":0}]}

【问题讨论】:

    标签: extjs jsonreader


    【解决方案1】:

    据我所知,Store 'id' 配置选项已被弃用。

    【讨论】:

      【解决方案2】:

      我认为 JsonStore 关闭记录 ID 是否执行 POST 与 PUT。因此,如果 id 不是用户生成的,它将发出 POST,否则发出 PUT。我指的是record.id而不是record.data.id。

      【讨论】:

      • 我已经告诉过 'id' 选项不再存在,所以应该改用 'storeId'。问题也与“POST vs PUT”无关。这家商店甚至都不安静。
      【解决方案3】:

      查看 Ext.data.Api 的来源,我会说动词搞砸了:

      restActions : { create : 'POST', read : 'GET', update : 'PUT', destroy : 'DELETE' },

      对我来说,创建应该是 PUT,更新应该是 POST。但我猜煎茶人有不同的想法。 顺便说一句,源码取自 Ext 3.3.1

      我想您可以覆盖 restActions 对象以按预期工作:

      Ext.data.Api.restActions = {
      create  : 'PUT',
      read    : 'GET',
      update  : 'POST',
      destroy : 'DELETE' };
      

      【讨论】:

        【解决方案4】:

        不确定第一部分(我的商店使用类似代码进行正确调用),但我可以阐明您问题的第二部分。

        Ext.data.JsonStore NOT 接受 reader 对象 - 它只接受字段并始终创建自己的 reader,正如您从源代码中看到的那样

        Ext.data.JsonStore = Ext.extend(Ext.data.Store, {
            constructor: function(config){
                Ext.data.JsonStore.superclass.constructor.call(this, Ext.apply(config, {
                    reader: new Ext.data.JsonReader(config)
                }));
            }
        });
        

        并且在 Ext 2.x 中被确认为“按照文档”

        http://www.sencha.com/forum/showthread.php?57189-2.x-Closed-Bug-in-Ext.data.JsonStore-cconstructor

        因此,如果您提供自己的阅读器,那么在这种情况下,您必须创建一个 Ext.data.Store(这也让我感到困惑)

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题。我解决了它在 json 存储中建立 Success 属性并在我的 create 方法的响应中返回成功 true 。这是我的代码。希望对你有帮助

          var EStore = new Ext.data.JsonStore
                          ({
                             api: { read: 'getAssignedJobs',
                                  create: 'createAssignedJobs',
                                  update: 'updateAssignedJobs',
                                  destroy: 'destroyAssignedJobs'},
                              root: 'jobData',
                              idProperty: 'Id',
                              autoSave: false,
                              autoLoad: true,
                              batch: true,
                              successProperty: 'success',
                              writer: new Ext.data.JsonWriter({ encode: true, writeAllFields: true }),
                              fields: [
                                  { name: 'Id', type: 'int', mapping: 'Id' },
                                  { name: 'ResourceId', type: 'int', mapping: 'fitter_id' },
                                  { name: 'StartDate', type: 'date', dateFormat: "Y-m-d\\TH:i:s" },
                                  { name: 'EndDate', type: 'date', dateFormat: "Y-m-d\\TH:i:s" },
                                  { name: 'status', type: 'int' },
                                  { name: 'job_id', type: 'int' }
                                          ]
                          });
          

          这是我从服务器端创建的方法..

          public JsonResult createAssignedJobs(string jobData)
              {
                  var Jsondata = (JobfitterToScheduler)new JavaScriptSerializer().Deserialize<JobfitterToScheduler>(jobData);
                  JobToFitterRepo jobtofitter = new JobToFitterRepo();
                  if (Jsondata != null)
                  {
                      jobtofitter.insertJobToFitter(13, Jsondata.fitter_id, 0, DateTime.Now, DateTime.Now);
                      return this.Json(new { success = true, jobData = Jsondata });
                  }
                  return null;
              }
          

          【讨论】:

            猜你喜欢
            • 2011-05-16
            • 1970-01-01
            • 2014-09-08
            • 2014-05-22
            • 2017-08-17
            • 2011-11-11
            • 2011-12-01
            • 2014-10-19
            • 2013-12-27
            相关资源
            最近更新 更多