使用从jsp获取的动态数据

时间:2012-04-11 06:48:34

标签: model-view-controller sencha-touch

在下面的代码中,我在嵌套列表中获取静态数据。但是,取代静态数据,我想要动态数据,即从jsp获取的数据。我不知道该怎么做。

Ext.define('SenchaApp.store.Items', {
    extend: 'Ext.data.TreeStore',

    config: {
        model: 'SenchaApp.model.Item',
        defaultRootProperty: 'items',
        root: {
            items: [
            {
                text: 'Categories1',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
            {
                text: 'Categories2',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
             {
                text: 'Categories3',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
             {
                text: 'Categories4',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
        ]
    }
    }
});

1 个答案:

答案 0 :(得分:0)

实际上你不必在这里使用JSONP。使用JSON读取器使用Ajax请求要容易得多。

根据您的代码,我认为您的模型定义应该是这样的:

Ext.define('SenchaApp.model.Item', {
        extend: 'Ext.data.Model',
        config: {
            fields: ['text'],
        }
});

然后,这将从服务器获取JSONP并填充您的商店:

Ext.define('SenchaApp.store.Items', {
    extend: 'Ext.data.TreeStore',

    config: {
        model: 'SenchaApp.model.Item',
        proxy: {
            type: 'ajax',
            url: enter_your_api_url_here,
            extraParams: {set your extra params if needed},
            reader: {
                type: 'json',
                rootProperty: 'items'
            }
        },
        autoLoad: true,
    }
});

希望它有所帮助。

PS:如果它不起作用,请复制&粘贴你的jsonp文件链接,我会帮助你。