Extjs存储DataModel子字段空错误

时间:2013-04-01 09:21:34

标签: extjs null store datamodel

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

Cannot read property 'id' of null

我的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: '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}]}

2 个答案:

答案 0 :(得分:0)

检查以下内容:

  1. 检查模型的命名空间。它只是ruleDataModel还是像foo.bar.ruleDataModel

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

答案 1 :(得分: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: ''}