ExtJS5: Get rid of root property in proxy

2019-04-11 03:38发布

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条回答
甜甜的少女心
2楼-- · 2019-04-11 04:00

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

rootProperty: function(raw) {
    return raw.items ? raw.items : raw;
}
查看更多
登录 后发表回答