How to load JSON file from Thingworx Repository
Hi all,
I have some JSON dataset in Thingworx repository. I'm looking for a way to download and use it in experience runtime. Could any one help me with this.
Thanks in advance.
/VR
Hi all,
I have some JSON dataset in Thingworx repository. I'm looking for a way to download and use it in experience runtime. Could any one help me with this.
Thanks in advance.
/VR
Yes this seems to be difficult to achieve. I think in your code is a problem with passing arguments between the call of the service and the event. It is correctly as your mentionded that this is asynchronous call – but nearly all calls in JavaScript are asynchronous calls. The $broadcast works asynchronously and will not return any values and there is no way to pass e.g. the key argument to the event LoadJSON-complete.
Also, I want to point that to define the event listener inside the forEach loop is definitely not a good idea. The event listener should be defined globally.
So I fixed the code and called the broadcast /also alternate call is twx.app.fn.triggerDataService (better) / - called the service in a loop for each key/value pair from the json list . So all calls are executed immediately (as expected) - An then after some time the events are fired- but there seems to be something a twx srv cache issue - so it seems to forget the other call - when we have more than 2 or 3 call so it did not emit the events for all calls.
To the question - how to make synchronous - I do not think that it is possible here. In generally we can use some promise approach but here we have an array as results and it will be difficult to manage the different items
For me seems that the best approach is to use some recursive call principle -
call - event completed - next call etc....
The following code was working fine in my example:
// $scope, $element, $attrs, $injector, $sce, $timeout, $http, $ionicPopup, and $ionicPopover services are available
$scope.modelDataDynamic=[];
$scope.COUNT=0;
var MODEL_JSON = {
'model-1-a': 'a.json',
'model-1-b': 'b.json',
'model-1-c': 'c.json',
'model-1-d': 'd.json',
'model-1-e': 'e.json',
'model-1-f': 'f.json',
}
//load MODEL_JSON files
console.log('load MODEL_JSON files');
//==================================================
//==================================================
angular.element(document).ready(function () {
console.log("Register the LoadJson-Complete");
//==== LoadJSON COMPLETE
$scope.$root.$on('LoadJSON-complete', function (event, args) {
console.log("LoadJSON-complete event");
//will print to console the event name - LoadJSON-Complete
console.log("name=" + event.name)
// the data was the event returns
$scope.modelDataDynamic[$scope.COUNT].jsonData = args.data;
//print to the console to check the data
console.log(JSON.stringify($scope.modelDataDynamic[$scope.COUNT].jsonData))
// debugging inof
console.log("key=" +$scope.modelDataDynamic[$scope.COUNT].key+";value="
+ $scope.modelDataDynamic[$scope.COUNT].value + ";COUNT="+$scope.COUNT+
"length="+$scope.modelDataDynamic.length);
// this a double check we will convert it to string and parse it back to json
//this will ensure that data is correct and has the correct syntax
if( ++$scope.COUNT > $scope.modelDataDynamic.length-1)
{//finished
$scope.finished();
}
else
{$timeout($scope.GetJsonFromTwxRepository('/test/jsons/'
+ $scope.modelDataDynamic[$scope.COUNT].value),500,true)
}
});
})
//===================================================
//==================================================
$scope.GetJsonFromTwxRepository = function (path) {
console.log("$scope.GetJsonFromTwxRepository(path="+path+") ");
$timeout(function () {
// $rootScope.$broadcast('app.mdl.TestRepository.svc.LoadJSON',
twx.app.fn.triggerDataService('TestRepository','LoadJSON',
{"path": path});}
, 5,true);
console.log("after call of GetJsonFromTwxRepository")
};
//==================================================
//==================================================
$scope.$on('$ionicView.afterEnter', function () {
console.log("$ionicView.afterEnter was called ");
console.log('Calling now the service in a loop');
$scope.COUNT=0;
angular.forEach(MODEL_JSON, function (value, key) {
console.log("calling angular.forEach value="+value+ " key="+key);
let temp_obj= {}
temp_obj.key=key
temp_obj.value=value
$scope.modelDataDynamic.push(temp_obj) })
$timeout(()=>{
$scope.GetJsonFromTwxRepository('/test/jsons/' +$scope.modelDataDynamic[$scope.COUNT].value)
$scope.$applyAsync();
},100,true)
})
//=======================$scope.finished();=========
//==================================================
//$scope.$watch('COUNT', function (newValue, oldValue, scope) {
//if(scope.app.params.SVC_CALL_NUM <1)
$scope.finished=function(){
console.warn("completed the loop");
let a = $scope.modelDataDynamic
for (var i in a)
{ console.log("here Json for $scope.modelDataDynamic["+i+"]")
console.log("key="+ a[i].key + " value="+a[i].value)
console.log("==============================================")
console.log(JSON.stringify(a[i].jsonData));
console.log("==============================================")
}
$timeout( function() { console.warn($scope.modelDataDynamic);}, 1000)
}
So when tested I was working fine with the following print:

Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.