I have to combine the data from three different sources. Ideally, I would do this on the web server, but I have to use JSON text files at this time until others are prepared to support the web service calls. As such, I have 3 JSON files that I need to loop through, and process the data for a page.
My problem is that the one structure I am getting initially, is not available later in my controller to process the data in. I know this should be moved to different controllers, and likely use factories/services, but I am trying to do a Proof of Concept to demo the available look/presentation to get approval to build the application. As such, quick and dirty is needed to simply get the approval for us to then break it and do it proper, with testing built in from the start.
My code:
var App = angular.module('App', []);
App.controller('Ctrl', function ($scope, $http)
{
$scope.ToPlay = 0;
$scope.Won = 0;
$scope.Lost = 0;
var Structure = [ "Col1", "Col2", "Col3" ];
$scope.Data = [];
$scope.JSON1 = [];
$scope.JSON2 = [];
$scope.JSON3 = [];
// Read the 3 files for processing
$http.get('Data/JSON1.json').success(function(data) {
$scope.JSON1 = data;
});
$http.get('Data/JSON2.json').success(function(data) {
$scope.JSON2 = data;
});
$http.get('Data/JSON3.json').success(function(data) {
$scope.JSON3 = data;
});
// Create Structure in Result
var Headers = [];
for( var i = 0; i < Structure.length; i++)
{
// Get the Value from Stucture for Columns
Headers.push(Structure[i]);
}
$scope.Data[0] = [];
$scope.Data[0].push(Headers);
// Get Key from JSON1 ID Field for Each Row
// Length of JSON1 is now 0 - This is the error
for( var i = 0; i < $scope.JSON1.length; i++)
{
var Key = $scope.JSON1[i].Id;
$scope.Data[Key] = [];
}
// Get Key from JSON2 Key Field for Each Row matches ID from JSON1
for( var i = 0; i < $scope.JSON2.length; i++)
{
var Record = $scope.JSON2[i];
$scope.Data[Record.Key].push({ "Date1": Record.Data1} );
}
});