Вытащил http-клиент в отдельный компонент

This commit is contained in:
2018-08-11 08:01:31 +02:00
parent 9e2accf33e
commit 39aeb66fff
3 changed files with 46 additions and 22 deletions

View File

@@ -3,6 +3,20 @@ import QtQml.Models 2.1
ListModel {
id: root
readonly property var httpClient: HttpClient {
id: httpClient
onError: {
root.error(text)
root.isLoading = false
}
onReply: {
root.populateModel(data)
root.isLoading = false
}
}
property string serviceUrl: undefined
property bool isLoading: false
@@ -11,29 +25,8 @@ ListModel {
onServiceUrlChanged: reload()
function reload() {
var request = new XMLHttpRequest()
request.open('GET', root.serviceUrl + '/static/channels.js')
request.onreadystatechange = function () {
if (request.readyState !== XMLHttpRequest.DONE) {
return
}
if (request.status === 200) {
populateModel(JSON.parse(request.responseText))
root.isLoading = false
return
}
root.error(qsTr("[%1] Request error: %2").
arg(request.status).
arg(request.statusText))
root.isLoading = false
}
root.httpClient.get(root.serviceUrl + '/static/channels.js')
root.isLoading = true
request.send()
}
function populateModel(data) {