0

当我尝试将远程数据加载到网格时,子字段出现此错误:

Cannot read property 'id' of null

我的数据模型:

    Ext.define('ruleDataModel', {
        extend: 'Ext.data.Model',
        fields: [
           { name: 'id'},
           { name: 'createTime', type:'date', dateFormat: 'timestamp', convert:function(v,j){ return (v != null?new Date(v):null);}},
           { name: 'discountPercent'},
           { name: 'discountAmount'},
           { name: 'discountOverSalePriceFlag', type: 'boolean'},
           { name: 'minSalePriceTotal'},
           { name: 'maxCount'},
           { name: 'execOrder'},
           { name: 'clearanceIncludedFlag', type: 'boolean'},
           { name: 'relatedProductMinCount'},
           { name: 'promocodeRuleTypeName', mapping: 'promocodeRuleType.friendlyType'},
           { name: 'groupName'},
           { name: 'productTypeId', mapping: 'productType.id', defaultValue: ''},
           { name: 'productTypeName', mapping: 'productType.name', defaultValue: ''},
           { name: 'relatedProductTypeId', mapping: 'relatedProductType.id', defaultValue: ''},
           { name: 'relatedProductTypeName', mapping: 'relatedProductType.name', defaultValue: ''}
        ], 
        idProperty: 'id'
    });

返回的 JSON 数据:

{totalCount: 1, root: [{"productType": {"name":
...
"relatedProductType":null,
...
"execOrder":0,"id":11}]}
4

2 回答 2

0

对于子字段,DataModel 应该这样定义:

Ext.define('ruleDataModel', {
            extend: 'Ext.data.Model',
            fields: [
               { name: 'id'},
               { name: 'createTime', type:'date', dateFormat: 'timestamp', convert:function(v,j){ return (v != null?new Date(v):null);}},
               { name: 'discountPercent'},
               { name: 'discountAmount'},
               { name: 'discountOverSalePriceFlag', type: 'boolean'},
               { name: 'minSalePriceTotal'},
               { name: 'maxCount'},
               { name: 'execOrder'},
               { name: 'clearanceIncludedFlag', type: 'boolean'},
               { name: 'relatedProductMinCount'},
               { name: 'promocodeRuleTypeName', mapping: 'promocodeRuleType.friendlyType'},
               { name: 'groupName'},
               { name: 'productType.id', mapping: 'productType', convert:function(v,j) { console.log(v,j); return (v != null?v.id:"");}},
               { name: 'productType.name', mapping: 'productType', convert:function(v,j) { console.log(v,j); return (v != null?v.name:"");}},
               { name: 'relatedProductType.id', mapping: 'relatedProductType', convert:function(v,j) { console.log(v,j); return (v != null?v.id:"");}},
               { name: 'relatedProductType.name', mapping: 'relatedProductType', convert:function(v,j){ console.log(v,j); return (v != null?v.name:"");}}
            ], 
            idProperty: 'id'
        });

不同的是

正确的那一个:

{ name: 'productType.id', mapping: 'productType', convert:function(v,j) { console.log(v,j); return (v != null?v.id:"");}},

错误一:

{ name: 'productTypeId', mapping: 'productType.id', defaultValue: ''}
于 2013-04-01T10:18:43.087 回答
0

检查以下事项:

  1. 检查模型的命名空间。它只是ruleDataModel还是类似的东西foo.bar.ruleDataModel

  2. 你的json可能有问题。有一种简单的方法可以测试 json 是否正确。将该 json 保存在 .json 文件中,然后在浏览器中打开该文件。如果浏览器能够正确打开该 json 文件,那么您的 json 是正确的,否则不是。

于 2013-04-01T09:24:53.667 回答