// Movements Controller
app.controller("MovementsCtrl", ["$scope", "$rootScope", "$filter", "$timeout", "$route", function($scope, $rootScope, $filter, $timeout, $route) {
$rootScope.pageName = "Movements";
var date = new Date();
var currentDate = $filter('date')(new Date(), 'MM/dd/yyyy');
$scope.labels = [];
$scope.data = [];
$scope.recordsArray = []; // Converts records object into an array.
console.log($scope.recordsArray);
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
$rootScope.userID = user.uid;
// Run query to pull user records.
$timeout(function() {
var userID = $rootScope.userID;
var query = firebase.database().ref('/users/' + userID +'/movements/').orderByChild('created');
query.on("value", function(snapshot) {
$scope.records = snapshot.val();
angular.forEach($scope.records, function(element) {
$scope.recordsArray.push(element);
$scope.labels.push(element.name);
$scope.data.push(element.weight);
});
});
});
} else {
console.log("not signed in");
}
});
$scope.addRecord = function() {
console.log("Pushed");
var recordID = database.ref().push().key;
var userID = $rootScope.userID;
database.ref('users/' + userID + '/movements/' + recordID).set({
user: userID,
name: "Front Squat",
sets: 5,
reps: 5,
weight: 350,
created: currentDate
});
$route.reload();
}
}]);
For some reason my page loads before the arrays in my JS load, rendering an empty page. I've tried wrapping everything in an init() function and loading that, but still the same issue. Can anyone help me figure out how to pre-load my JS arrays beforehand or is there another solution?
Thanks.