Вытащил http-клиент в отдельный компонент
This commit is contained in:
30
HttpClient.qml
Normal file
30
HttpClient.qml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import QtQuick 2.0
|
||||||
|
|
||||||
|
QtObject {
|
||||||
|
id: root
|
||||||
|
|
||||||
|
signal reply(var data)
|
||||||
|
signal error(string text)
|
||||||
|
|
||||||
|
function get(url) {
|
||||||
|
var request = new XMLHttpRequest()
|
||||||
|
|
||||||
|
request.open('GET', url)
|
||||||
|
request.onreadystatechange = function () {
|
||||||
|
if (request.readyState !== XMLHttpRequest.DONE) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.status === 200) {
|
||||||
|
root.reply(JSON.parse(request.responseText))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
root.error(qsTr("[%1] Request error: %2").
|
||||||
|
arg(request.status).
|
||||||
|
arg(request.statusText))
|
||||||
|
}
|
||||||
|
|
||||||
|
request.send()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,20 @@ import QtQml.Models 2.1
|
|||||||
ListModel {
|
ListModel {
|
||||||
id: root
|
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 string serviceUrl: undefined
|
||||||
property bool isLoading: false
|
property bool isLoading: false
|
||||||
|
|
||||||
@@ -11,29 +25,8 @@ ListModel {
|
|||||||
onServiceUrlChanged: reload()
|
onServiceUrlChanged: reload()
|
||||||
|
|
||||||
function reload() {
|
function reload() {
|
||||||
var request = new XMLHttpRequest()
|
root.httpClient.get(root.serviceUrl + '/static/channels.js')
|
||||||
|
|
||||||
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.isLoading = true
|
root.isLoading = true
|
||||||
request.send()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function populateModel(data) {
|
function populateModel(data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user