我正在研究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非常感謝那些有用的陳述。我根據你的陳述重構了面板。現在它工作得很好。 –
最受歡迎:) –