Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Basics (1.x) Improving Our Todo App Finalizing Our Application

re-setting the todo.edited back boolean "false" after save?

hi Huston Hedinger & all,

so i've been trying to add a feature to the todos project i feel it needs, but have been unsuccessful in making it happen. i thought i'd ask here for help?

it struck me that each time you save, it keep incrementing and saving the entire history of todos that have ever been edited, even if you haven't made any changes to the previous todos you edited in this later round of saving. so consequently, the feedback you get in the console.log() is 3 todos have been saved, the first time, then, if you edit and save two more todos, you get 5 todos have been saved. it struck me it should reset each time and just save & notify on the latest todos saved, as in the second time you should see 2 todos have been saved.

however, i can't get it to work.

i tried putting a for... in loop in either services/data.js this.saveTodos or in controllers/main.js $scope.saveTodos that, after most of the other logic, re-set the todos that had todo.edited now set to boolean true back to boolean false. but each time i try, i'd get an error in the console from angular.js saying i can't alter it as it is a readonly object.

is there some other ng-directive i should use? if so, which one? some way to make the object read-write, and then switch it back to read-only after writing to it? please let me know.

best,

ā€” faddah

 portland, oregon, u.s.a.
akak
akak
29,445 Points

can you share a workspace snapshot?

3 Answers

I would recommend adding just one line of code (todo.edited = false) in the filter callback function:

$scope.saveTodos = function() {
    var filteredTodos = $scope.todos.filter(function(todo) {
    if(todo.edited) {
        todo.edited = false;
        return todo;
    }
    });
    console.log(filteredTodos);
    dataService.saveTodos(filteredTodos);
};

How does this work for everyone?

Hello World
Hello World
12,904 Points

Hi, I felt the same way that after you click save button, the "EDITED" should be gone. So I simply changed the $scope.saveTodos in main.js to the below, and it works fine.

$scope.saveTodos = function() {
        var savedTodos = $scope.todos.map(function(todo) {
            if(todo.edited) {
                todo.edited = false;
                return todo;
            }
        });

        dataService.saveTodos(savedTodos);

    };

However, in this way, if you follow the teacher to log the savedTodos.length, it's gonna be 6 all the time because it iterates the whole array.

Patrick O'Dacre
Patrick O'Dacre
15,471 Points

I actually reset the 'edited' property in my service instead of my controller.

like so:

this.saveTodos = function (todos) {
        // if my array isn't empty, confirm save.
        if (todos.length > 0) {
            for (var i = 0; i < todos.length; i++) {
                console.log(todos[i].name + " has been saved! ");
                todos[i].edited = false;
            }
        } else {
            console.log("There are no changes to save.");
        }
    };