ExtJS5: Get rid of root property in proxy

不羁的心 提交于 2019-12-06 08:59:28

问题


I'm trying to connect a REST API to my ExtJS application.

For GET /user alike requests I return a response as follows:

{items: [{id: 1, ...}, {id: 2, ....}], total: 2}

So I created a model for that:

Ext.define('model.User', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'name' },
    ],

    proxy: {
        reader: {
            type:          'json',
            totalProperty: 'total',
            rootProperty:  'items'
        },
        type:   'rest',
        url:    '/Api/User',
    }
});

The grids load data and all look perfect. Now I want to be able to request a single record which my api serves as {id: 1, ...}. But when I do model.User.load(1) the success handler is never triggered because the response doesn't contain items property. If I put my record in that property, it will work but also will look ugly for other API users.

How can I make it work without the root property? I can't find any events for proxy/reader on a model to change it dynamically.


回答1:


The rootProperty can also be a function, so you could do something like:

rootProperty: function(raw) {
    return raw.items ? raw.items : raw;
}


来源:https://stackoverflow.com/questions/26824808/extjs5-get-rid-of-root-property-in-proxy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!