在Sencha Touch 2中的商店中使用代理

时间:2012-12-04 00:35:33

标签: sencha-touch sencha-touch-2

我在这个项目中的目标是加载由Web服务器上的脚本输出的Json,这样我就可以在Sencha Touch 2的List中显示数据。

我看过无数的例子,包括本网站上其他人的问题的答案,我仍然无法弄清楚我的代码存在什么问题。我确信这是一个非常小的或者可能是我不知道的规则,但我希望有人可以指出我正确的方向。

这是我的模特:

Ext.define('Sencha.model.Location', {
    extend: 'Ext.data.Model',
    config: {
        fields: ['name','location','open','details']
    }
});

这是我的商店:

Ext.define('Sencha.store.Locations',{
    extend: 'Ext.data.Store',
    requires:[
        'Sencha.model.Location',
    ],
    config: {
        model: 'Sencha.model.Location',
        storeId: 'Locations',
        proxy: {
      type: 'ajax',
      url : 'http://url/to/locations.php?filetype=.json',
      reader: {
        type: 'json',
      },
      autoLoad: 'true'
    }
    }
});

以下是我希望它显示的视图:

Ext.define('Sencha.view.LocationList',{
    extend: 'Ext.List',
    alias: 'widget.LocationList',
    xtype: 'locationlist',
    config: {
        title: 'What\'s Open @CU',
        disableSelection: true,
        itemTpl: '<img src="http://localhost/{open}.png" style="width:16px;height:16px;margin-right:8px;" />{name}<span style="font-size:9pt;margin-left:8px;color:#888;">{location}</span>',
        store: 'Locations',
        onItemDisclosure: true
    }
});

这是输出的JSON(可能是格式化问题导致它无声地失败?)

{
"businesses":
    {
    "name" : "Baker's"
    "location" : "4th Floor Uni Centre"
    "open" : "open"
    "details" : "This is some information."
    }
}

3 个答案:

答案 0 :(得分:1)

在商店的rootProperty:'businesses'添加reader: { type: 'json', rootProperty:'businesses' }

答案 1 :(得分:1)

你JSON无效。你忘记了逗号:

{
"businesses":
    {
    "name" : "Baker's",
    "location" : "4th Foor Uni Centre",
    "open" : "open",
    "details" : "This is some information."
    }
}

此外,如果您打算发送多个商家,您可能希望将JSON格式更改为以下内容:

{
"businesses":[
    {
    "name" : "Baker's",
    "location" : "4th Foor Uni Centre",
    "open" : "open",
    "details" : "This is some information."
    },
    {
    "name" : "Baker's",
    "location" : "4th Foor Uni Centre",
    "open" : "open",
    "details" : "This is some information."
    }
    ...
]
}

然后将rootProperty: 'businesses'添加到代理人的读者身上,就像nuthan所说的那样。

希望这有帮助

答案 2 :(得分:0)

http://url/to/locations.php 

与您的sencha触控应用程序相同的位置? 如果没有,则需要JsonP代理。 JsonP将Json数据包装到像上下文这样的函数中以使其可用。

相关问题