JsonStore 和 JsonReader 的问题

发布于 2024-09-05 06:38:45 字数 4001 浏览 7 评论 0原文

在我的 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}]}

In my ExtJS application I use EditorGridPanel to show data from server.

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}]
    });

When I use JsonStore without reader it works, but when I try to update any field it uses my 'create' url instead of 'update'.

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
    })
});

I believe that the problem is that I don't use reader, but when I use JsonReader grid stops showing any data at all.

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
    })
});

So, does anyone know what the problem might be and how to solve it. The data returned from my server is Json-formated and seams to be ok

{"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}]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

長街聽風 2024-09-12 06:38:45

我遇到了同样的问题..我解决了它在 json 存储中建立 Success 属性并在我的创建方法的响应中返回 success 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;
    }

I had the same problem.. I solved it establishing Success Property in the json store and returning success true in the response from my create Method.. here is my code. Hope it helps

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' }
                                ]
                });

and this is my Create method from Server Side..

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;
    }
童话 2024-09-12 06:38:45

看看 Ext.data.Api 的源代码,我会说动词都搞砸了:

restActions : {
创建:'发布',
读:“获取”,
更新:'放置',
销毁:'删除'
对我来说,

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

我想你可以重写 restActions 对象以按你期望的方式工作:

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

Looking at the source for Ext.data.Api I would say that the verbs are screwed up:

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

For me the create should be a PUT and the update should be a POST. But I guess the Sencha guys have a different idea.
Btw, the source is taken from Ext 3.3.1

I guess you could override the restActions object to work as you would expect:

Ext.data.Api.restActions = {
create  : 'PUT',
read    : 'GET',
update  : 'POST',
destroy : 'DELETE' };
风透绣罗衣 2024-09-12 06:38:45

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

As I've managed to know the Store 'id' config option is depricated.

橘虞初梦 2024-09-12 06:38:45

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

I think the JsonStore keys off the record id on whether to do a POST vs PUT. So if the id is non-user generated the it will issue a POST, and a PUT otherwise. I am referring to record.id not record.data.id.

無處可尋 2024-09-12 06:38:45

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

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

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 (这也让我感到困惑)

Not sure about the first part (my stores make the correct calls with similar code) but I can shed light on the 2nd part of your question.

Ext.data.JsonStore does NOT accept a reader object - it only takes fields and always makes its own reader, as you can see from the source

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)
        }));
    }
});

and is confirmed as being "as per docs" in Ext 2.x

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

So if you supply your own reader you must, in this case, make an Ext.data.Store instead (this caught me out too)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文