I'm still getting my head around AngularJS, so my question may be flawed, but is it possible to delay the execution of a controller until the json files are loaded (in a different function)?
Here's the controller:
app.controller('progressCtrl', ['$scope', '$routeParams', '$http', 'content', function($scope, $routeParams, $http, content) {
function initScope() {
$scope.pageId = $routeParams.pageId;
$scope.totalPages = 0;
content.success(function(data) {
$scope.pages = data;
angular.forEach($scope.pages, function(value, key) {
if ($scope.totalPages <= value.id) $scope.totalPages = value.id;
if ($scope.pageId == value.id) {
value.class = "active";
} else {
value.class = "";
}
if (incompleteObjectives.length>0) {
var matchFound = false;
for (var i=0;i<incompleteObjectives.length-1;i++) {
if (incompleteObjectives[i].indexOf('page-'+value.id+'-')) {
matchFound = true;
}
}
if (!matchFound) {
value.class += " completed";
}
} else {
value.class += " completed";
}
});
});
}
initScope();
}]);
And here is the factory that calls the 'getData' function, which is where the json files are loaded...
/*Factory that runs http.get on content.json, used by multiple controllers */
app.factory('content', ['$http', function($http) {
var url = 'json/content.js';
return $http.get(url)
.success(function(data) {
for (var i = 0; i < data.length; i++) {
numberOfPages += 1;
}
// load page .js files
var tempData = getData(0);
for (var i = 1; i < numberOfPages; i++) {
(function(i) {
tempData = tempData.then(function() {
return getData(i);
});
}(i));
}
return data;
})
.error(function(data) {
return data;
});
}]);
// loads individual page data, stores in pageData
function getData(id) {
return $.ajax({
url: 'json/page' + (id + 1) + '.js',
dataType: 'json'
}).done(function(d) {
if(firstRun)
addPageObjectives(id, d)
// push data into pageData array, for use later when pages are routed to
console.log("pushing page data into pageData for page "+id);
pageData.push(d);
}).fail(function() {
// console.log('ERROR loading page index ' + id);
});
}
For completion's sake, here's the angular html, which features an ng-class:
<div ng-if="content.titletext!=undefined" class="sidetitle" ng-controller="progressCtrl">
<div class="container">
<div>
{{content.titletext}}
</div>
<div class="progress-buttons">
<div ng-repeat="(skey,page) in pages" class="button {{skey+1}}" ng-class="page.class" ng-attr-id="{{'nav-'+(skey+1)}}">
<div ng-switch="page.titletext || '_undefined_'">
<span ng-switch-when="_undefined_">{{skey+1}}</span>
</div>
</div>
</div>
</div>
<div class="container">
<div ng-if="content.chapterTitleText!=undefined" class="side-chapter-title">
{{content.chapterTitleText}}
</div>
<div class="progress-pages">
<span>{{pageId}} of {{totalPages}}</span>
</div>
</div>
</div>
Ultimately it's the pageData array I want to access but, at the point the progress controller is running only the first json file has loaded so I can only access details from the first page. I'd like to have the code in progressController run when all the json files are loaded - and still have access to the value.class property.
Is that possible?
Thanks.
UPDATE
From an answer below, here's what I've tried. I know the following is wrong, but I'd like some tips on how it's wrong, if possible.
/*Factory that runs http.get on content.json, used by multiple controllers */
app.factory('content', ['$http', function($http) {
var promise;
var url = 'json/content.js';
function init() {
console.log("Good news! content factory init function does get called!");
promise = $http.get(url).then(function success(data) {
for (var i = 0; i < data.length; i++) {
numberOfPages += 1;
}
console.log("data.length = "+data.length); // this outputs as 'undefined'; I'm not sure why, given loading was, presumably, successful
// load page .js files
var tempData = getData(0);
for (var i = 1; i < numberOfPages; i++) {
(function(i) {
tempData = tempData.then(function() {
return getData(i);
});
}(i));
}
return data;
}); // I took off the .error lines because they threw an error - I'm assuming the syntax didn't match the new 'success' syntax above
}
init(); // I added this - not sure if that's right, but otherwise the init function didn't run...
function getData(id) {
return $.ajax({
url: 'json/page' + (id + 1) + '.js',
dataType: 'json'
}).done(function(d) {
if(firstRun)
addPageObjectives(id, d)
// push data into pageData array, for use later when pages are routed to
console.log("pushing page data into pageData for page "+id);
pageData.push(d);
}).fail(function() {
// console.log('ERROR loading page index ' + id);
});
return promise;
}
// public API
return {
getData: getData
}
}]);
Elsewhere, in the controllers I'm using lines, such as:
content.getData().then(function(data) {
....
}
...to access the page data (which, I think is not working). I'm guessing this update is wrong in a lot of places, but if folks are willing to help, that would be great. Thanks again.