After seeking help from @Kato on Stack Overflow regarding sorting a Firebase AngularFire array into weeks and days, I encountered an error where I cannot set a property of "something" that is undefined.
To provide more context, I created a detailed Plunker: http://plnkr.co/edit/M4zEjpZ4kqTKn1sHHMt6
//controller
angular.module('app').controller('DemoCtrl', function($scope, DatedList) {
$scope.world = 'world';
var listRef = new Firebase("https://talllly.firebaseio.com/");
$scope.weeks = DatedList(listRef);
$scope.addTask = function(){
listRef.push({
text: $scope.task.text,
week: 40,
day: 2
});
};
});
angular.module('app').service('DatedList', function($timeout) {
return function(pathToList) {
var list = {};
pathToList.on('child_added', function(snap) {
$timeout(function() { // force Angular to run $digest when changes occur
var data = snap.val();
console.log(data);
var week_number = data.week;
var week_day = data.day;
list[week_number][week_day] = data;
});
});
//todo: write similar processing for child_changed and child_removed
return list;
}
});
//html
<form ng-submit="addTask()">
<input placeholder="add task" ng-model="task.text">
</form>
<div ng-repeat="(week, days) in weeks">
<h1>{{week}}</h1>
<div ng-repeat="(day, items) in days">
<h2>{{day}}</h2>
<div ng-repeat="item in items">
{{item|json}}
</div>
</div>
</div>