How to read nested JSON structure with a Sencha Touch Data Model?

前端 未结 2 1503
失恋的感觉
失恋的感觉 2021-02-03 13:18

I\'ve been trying to figure this out all evening but to no avail. I have a JSON structure as follows (coming from another system so I can\'t change its structure):


          


        
2条回答
  •  自闭症患者
    2021-02-03 13:42

    I've added a converter to allow the template access the data in the model consistently regardless if a single object or an array is returned.

    Ext.regModel("ParentModel", {
            fields: [
                {name: 'parentId', type: 'string'},
                {name: 'children', convert: 
                function(value, record) {
                    if (value.child) {
                        if (value.child instanceof Array) {
                            return value.child;
                        } else {
                            return [value.child]; // Convert to an Array 
                        }
                    }
    
                    return value.child;
                }
            }
            ],
    
            proxy: {
                type: 'ajax',
                url : 'models.json',
                reader: {
                    type: 'json',
                    root: 'parents.parent' // this works fine
                }
            }
        });
    

    Note: I don't actually need to define the ChildrenModel. I guess I can get away without defining it as Sencha must be automatically type converting it.

提交回复
热议问题