我正在研究Sencha的Admin Dashboard样品并试图定制'天气'面板。如何使用tpl配置将json数据绑定到屏幕?
我已经使用OpenWeatherMap的Current weather data API创建了JSON格式的网址。我无法通过tpl
配置将JSON数据绑定到Weather面板。我已经创建了一个ViewModel并在Component中调用它,但它没有工作。
这里是组件类;
Ext.define('OWeb.view.dashboard.Weather', {
extend: 'Ext.Component',
xtype: 'weather',
baseCls: 'weather-panel',
border: false,
height: 80,
store: {
proxy: {
type: 'ajax',
url: 'http://api.openweathermap.org/data/2.5/weather?q=Antalya,TR&appid=9b59049894d42af608baf69f869b9ace&units=metric',
reader: {
type: 'json'
}
},
autoLoad: true
},
tpl: '<div class="weather-image-container"><img src="resources/img/{icon}" alt="{weather.description}"/></div>'+
'<div class="weather-details-container">' +
'<div>{main.temp}°</div>' +
'<div>{weather.main}</div>' +
'</div>'
});
这是link用于经由OpenWeatherMap和片断返回JSON数据;
{
"coord": {
"lon": 30.72,
"lat": 36.77
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 25,
"pressure": 1015,
"humidity": 23,
"temp_min": 25,
"temp_max": 25
},
"visibility": 10000,
"wind": {
"speed": 5.7,
"deg": 320
},
"clouds": {
"all": 0
},
"dt": 1507184400,
"sys": {
"type": 1,
"id": 6028,
"message": 0.0025,
"country": "TR",
"sunrise": 1507175759,
"sunset": 1507217657
},
"id": 323776,
"name": "Antalya",
"cod": 200
}
谢谢,任何建议是值得欢迎的。
UPDATE
我发现this post,我尝试同样的事情;从Ext.DataView
延伸,设置代理类型jsonp
并使用itemTpl
配置。现在我可以绑定到JSON数据,但只能显示{main.temp}
。请任何想法吗?
Ext.define('OWeb.view.dashboard.Weather', {
//extend: 'Ext.Component',
extend: 'Ext.DataView',
xtype: 'weather',
baseCls: 'weather-panel',
border: false,
height: 80,
store: {
proxy: {
type: 'jsonp',
url: 'http://api.openweathermap.org/data/2.5/weather?q=Antalya,TR&appid=9b59049894d42af608baf69f869b9ace&units=metric',
reader: {
type: 'json'
}
},
autoLoad: true
},
itemTpl: '<div class="weather-image-container"><img src="{weather.icon}" alt="{weather.description}"/></div>'+
'<div class="weather-details-container">' +
'<div>{main.temp}°</div>' +
'<div>{weather.description}</div>' +
'</div>'
});
亲爱的@Amita非常感谢那些有用的陈述。我根据你的陈述重构了面板。现在它工作得很好。 –
最受欢迎:) –