From d977d7be158b7062d2b23b38cd85d4117b3e486d Mon Sep 17 00:00:00 2001 From: think-free Date: Fri, 7 Feb 2014 13:52:44 +0100 Subject: [PATCH 1/3] Update JSONListModel.qml Intelligent update of the elements (without destroying everything each time the model is updated). You have to specify which is the unique identifier of the element : 'propId' of the JSONListModel --- JSONListModel/JSONListModel.qml | 89 +++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 9 deletions(-) diff --git a/JSONListModel/JSONListModel.qml b/JSONListModel/JSONListModel.qml index 62b466d..07b3ed7 100644 --- a/JSONListModel/JSONListModel.qml +++ b/JSONListModel/JSONListModel.qml @@ -4,16 +4,24 @@ * Licensed under the MIT licence (http://opensource.org/licenses/mit-license.php) */ -import QtQuick 1.1 +import QtQuick 2.0 import "jsonpath.js" as JSONPath Item { property string source: "" property string json: "" + property string jsonAnt: "" property string query: "" - + property string propId: "" // If this property is defined (unique identifier of each object) we'll update only the changes without destroying everything + property ListModel model : ListModel { id: jsonModel } property alias count: jsonModel.count + function get(index){ + + return jsonModel.get(index); + } + + signal jsonModelChanged; onSourceChanged: { var xhr = new XMLHttpRequest; @@ -29,16 +37,79 @@ Item { onQueryChanged: updateJSONModel() function updateJSONModel() { - jsonModel.clear(); + var objectArray; + var jo; + if (json === "") + { + jsonModel.clear(); + } + else if (propId === "" || jsonAnt === "") + { + jsonModel.clear(); + objectArray = parseJSONString(json, query); + for (var key in objectArray ) { + jo = objectArray[key]; + jsonModel.append(jo); + } + } + else + { + console.log("option 3"); + var i, j, id, found; + objectArray = parseJSONString(json, query); + var objectArrayAnt = parseJSONString(jsonAnt, query); - if ( json === "" ) - return; + //detecto los insertados y modificados + for (i = 0; i < objectArray.length; ++i ) { + id = objectArray[i][propId]; + found = false; + for (j = 0; j < objectArrayAnt.length; ++j) { + if (id === objectArrayAnt[j][propId]) + { + found = true; + break; + } + } - var objectArray = parseJSONString(json, query); - for ( var key in objectArray ) { - var jo = objectArray[key]; - jsonModel.append( jo ); + if (!found) + { + console.log("insert " + id); + jsonModel.append(objectArray[i]); + } + else + { + if (JSON.stringify(objectArray[i]) !== JSON.stringify(objectArrayAnt[j])) + { + console.log("modified " + id + " index " + j); + //jsonModel.set(j, objectArray[i]); + jsonModel.remove(j); + jsonModel.insert(j, objectArray[i]); + } + } + } + + //detecto los borrados + for (i = objectArrayAnt.length - 1; i >= 0; --i ) { + id = objectArrayAnt[i][propId]; + console.log(id); + found = false; + for (j = objectArray.length - 1; j >= 0; --j ) { + if (id === objectArray[j][propId]) + { + found = true; + break; + } + } + if (!found) + { + console.log("deleted " + id + " index " + i); + jsonModel.remove(i); + } + } } + + jsonModelChanged(); + jsonAnt = json; } function parseJSONString(jsonString, jsonPathQuery) { From a931d5cde97e3c821559047b4fff97f4406995a0 Mon Sep 17 00:00:00 2001 From: think-free Date: Thu, 13 Feb 2014 11:13:14 +0100 Subject: [PATCH 2/3] Update JSONListModel.qml --- JSONListModel/JSONListModel.qml | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/JSONListModel/JSONListModel.qml b/JSONListModel/JSONListModel.qml index 07b3ed7..940622f 100644 --- a/JSONListModel/JSONListModel.qml +++ b/JSONListModel/JSONListModel.qml @@ -10,18 +10,15 @@ import "jsonpath.js" as JSONPath Item { property string source: "" property string json: "" - property string jsonAnt: "" property string query: "" property string propId: "" // If this property is defined (unique identifier of each object) we'll update only the changes without destroying everything property ListModel model : ListModel { id: jsonModel } property alias count: jsonModel.count - function get(index){ - - return jsonModel.get(index); - } - + signal jsonModelChanged; + + jsonAnt: "" onSourceChanged: { var xhr = new XMLHttpRequest; @@ -59,7 +56,8 @@ Item { objectArray = parseJSONString(json, query); var objectArrayAnt = parseJSONString(jsonAnt, query); - //detecto los insertados y modificados + // Detect new and modified elements + for (i = 0; i < objectArray.length; ++i ) { id = objectArray[i][propId]; found = false; @@ -81,17 +79,17 @@ Item { if (JSON.stringify(objectArray[i]) !== JSON.stringify(objectArrayAnt[j])) { console.log("modified " + id + " index " + j); - //jsonModel.set(j, objectArray[i]); + // jsonModel.set(j, objectArray[i]); // Don't because set() merges properties instead of replacing jsonModel.remove(j); jsonModel.insert(j, objectArray[i]); } } } - //detecto los borrados + // Detect the removed elements + for (i = objectArrayAnt.length - 1; i >= 0; --i ) { id = objectArrayAnt[i][propId]; - console.log(id); found = false; for (j = objectArray.length - 1; j >= 0; --j ) { if (id === objectArray[j][propId]) From db27369a5ac1b7817d9c1d34c4bdcf89cbd1c4e0 Mon Sep 17 00:00:00 2001 From: think-free Date: Thu, 13 Feb 2014 11:27:09 +0100 Subject: [PATCH 3/3] Update JSONListModel.qml --- JSONListModel/JSONListModel.qml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/JSONListModel/JSONListModel.qml b/JSONListModel/JSONListModel.qml index 940622f..d4efebd 100644 --- a/JSONListModel/JSONListModel.qml +++ b/JSONListModel/JSONListModel.qml @@ -18,7 +18,7 @@ Item { signal jsonModelChanged; - jsonAnt: "" + property string jsonAnt: "" onSourceChanged: { var xhr = new XMLHttpRequest; @@ -51,7 +51,6 @@ Item { } else { - console.log("option 3"); var i, j, id, found; objectArray = parseJSONString(json, query); var objectArrayAnt = parseJSONString(jsonAnt, query); @@ -71,15 +70,16 @@ Item { if (!found) { - console.log("insert " + id); jsonModel.append(objectArray[i]); } else { if (JSON.stringify(objectArray[i]) !== JSON.stringify(objectArrayAnt[j])) { - console.log("modified " + id + " index " + j); - // jsonModel.set(j, objectArray[i]); // Don't because set() merges properties instead of replacing + // Don't do the following because set() merges properties instead of replacing + // Instead we remove it and insert it + // jsonModel.set(j, objectArray[i]); + jsonModel.remove(j); jsonModel.insert(j, objectArray[i]); } @@ -100,7 +100,6 @@ Item { } if (!found) { - console.log("deleted " + id + " index " + i); jsonModel.remove(i); } }