I have a controller which contains a $scope object like so:

$scope.ProjectEuler.Ctrl: {
    problems: [//List of problems]

In the array above I have listed everything related to that particular Project Euler problem. A typical object in that array looks like this:

    id: '1',
    title: 'Mutiples of 3 and 5',
    description: 'description of problem',
    isCollapsed: true,
    logic: function() {
        // List of functions not on $scope that solve the problem
    solution: {
        answer: '',
        msg: {
            text: '',
            type: INFO
        time: ''

Finally I have functions that can be used by all the objects above for displaying the answer, each of which requires access to the $scope to determine where the answer is placed - like so:

$scope.ProjectEulerCtrl.functions = {
        setMessage: function (index, msg, severity) {
            // Replace with this?
            $scope.ProjectEulerCtrl.problems[index].solution.msg.text = msg;
            $scope.ProjectEulerCtrl.problems[index].solution.msg.type = severity;
        resetSolution: function (index) {
            $scope.ProjectEulerCtrl.problems[index].solution.answer = '';
            $scope.ProjectEulerCtrl.problems[index].solution.msg.text = '';
            $scope.ProjectEulerCtrl.problems[index].solution.time = '';
        setSolution: function (index, answer, msg, time) {
            $scope.ProjectEulerCtrl.problems[index].solution.answer = answer;
            $scope.ProjectEulerCtrl.problems[index].solution.msg.text = msg;
            $scope.ProjectEulerCtrl.problems[index].solution.time = time;

So above I have another object that contains all the functions that can be used in the array - this attaches at the same level as the 'problems' array.


Is this considered bad practice to have everything attached to the one object in this way?

I know that if I split everything out and just had (for example) $scope.setMessage() it would be on the controller scope anyway and that I have introduced another object to the mix.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.