ExtJS5:删除代理中的根属性 [英] ExtJS5: Get rid of root property in proxy

查看:121
本文介绍了ExtJS5:删除代理中的根属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将REST API连接到我的ExtJS应用程序。

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

对于 GET / user 请求我回复如下:

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

所以我创建了一个模型: / p>

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

网格加载数据,看起来完美无缺。现在我想要请求一个记录,我的api用作 {id:1,...}
但是当我做 model.User.load(1)成功处理程序从不被触发,因为响应不包含 items 属性。如果我把我的记录放在该属性中,它将会工作,但对其他API用户来说也是丑陋的。

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.

推荐答案

rootProperty 也可以是一个函数,所以你可以这样做:

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

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

这篇关于ExtJS5:删除代理中的根属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆