ExtJS - 使用JsonReader无效安全地检索复杂对象 [英] ExtJS - null safe retrieval of complex objects using JsonReader

查看:102
本文介绍了ExtJS - 使用JsonReader无效安全地检索复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JsonReader将Json数据映射到要在网格/表单中使用的变量。后端是Java,有复杂的对象我Jsonify并传递到ExtJS前端。
这是我的JsonReader的一部分,它试图检索一个嵌套对象 -

I am using a JsonReader to map Json data to variables to be used in a grid/form. The back end is in Java and there are complex objects which I Jsonify and pass to the ExtJS front end. This is a part of my JsonReader which tries to retrieve a nested object -

{name:'status', type: 'string', mapping: 'status.name'}

当状态有一个值(服务器中不为空),但当状态为空时,网格加载失败。目前,我的工作是从服务器发送一个空的对象,以防空,但我认为应该有一种方法来处理这个在ExtJS。请在ExtJS方面建议一个更好的解决方案。

This works fine when status has a value (not null in the server), but the grid load fails when status is null. Currently the work around I have is to send an empty object from the server in case of null, but I assume there should be a way to handle this in ExtJS. Please suggest a better solution on the ExtJS side.

推荐答案

我可以想到两种可能性 - 一种记录,一种是无文件的: p>

I can think of two possibilities - one documented and one undocumented:


  1. 使用 convert() ://dev.sencha.com/deploy/dev/docs/?class = Ext.data.Fieldrel =nofollow> Ext.data.Field

  1. use the convert()-mechanism of Ext.data.Field:

{
    name:'status', 
    mapping: 'status',
    convert: function(status, data) {
        if (!Ext.isEmpty(status) && status.name) {
            return status.name;
        } else {
            return null;
        }
    }
}


  • 映射属性也可以采用一个提取器函数(这是没有文档的,所以也许这可能有点冒险依赖于这一点):

  • The mapping property can also take an extractor function (this is undocumented so perhaps it may be a little bit risky to rely on this):

    {
        name:'status', 
        mapping: function(data) {
            if (data.status && data.status.name) {
                return data.status.name;
            } else {
                return null;
            }
        }
    }
    


  • 这篇关于ExtJS - 使用JsonReader无效安全地检索复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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