0

I'm new to Durandal and haven't had much luck applying ko.computed methods inside of a viewmodel. Can someone tell me what the correct syntax or pattern is?

You can see the entire project at https://github.com/robksawyer/durandal-todo/blob/master/views/todos.html.

Each of the computeds that I apply gets the following error during a bind.

Error("Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.")

compose method requesting the viewmodel and view:

<!--ko compose: { 
        model: router.activeItem, //wiring the router
        afterCompose: router.afterCompose, //wiring the router
        cacheViews: false, //telling composition to keep views in the dom, and reuse them (only a good idea with singleton view models)
        transition: 'fadein'
    }--><!--/ko-->

viewmodel:

// count of all completed todos
    var completedCount = ko.computed(function () {
        return ko.utils.arrayFilter(todos(), function (todo) {
            return todo.completed();
        }).length;
    });

view https://github.com/robksawyer/durandal-todo/blob/master/views/todos.html

Error screenshot enter image description here

Rob Sawyer
  • 2,163
  • 1
  • 24
  • 25
  • The problem is not with your compose but with your view. Plase post the view code where do you use the `completedCount`! – nemesv May 15 '13 at 04:58
  • Added a project link along with a link directly to the view. – Rob Sawyer May 15 '13 at 05:22
  • I can't see anything wrong with your code, or your HTML; if it's the same problem as [your other question](http://stackoverflow.com/questions/16550727/durandal-todomvc-cannot-write-a-value-to-a-ko-computed) you may just be misinterpreting what the console is telling you. Could you post another screenshot? – gerrod May 15 '13 at 10:27
  • You could be right about misinterpreting the console message. I'm new to Knockout and Durandal. I've attached another screenshot above. Thanks. – Rob Sawyer May 16 '13 at 05:13
  • Yep, you're all good. If you're getting an error with the computed it will show up in the console in red, too. – gerrod May 17 '13 at 03:20

2 Answers2

0

Defining the vm singleton first and then adding the ko.computed methods will most probably take care of that error message.

var vm = {
   current : current,
   todos: todos,
   ... // remove ko.computeds from the singleton
};

vm.completedCount = ko.computed(function () {
    return ko.utils.arrayFilter(todos(), function (todo) {
        return todo.completed();
    }).length;
}, vm);

// add other ko.computeds

return vm;
RainerAtSpirit
  • 3,723
  • 1
  • 17
  • 18
  • I've implemented this and I'm still getting the same error in the console. Check out http://postimg.org/image/ybi19a099/. This shouldn't be showing up, correct? I've tagged a version of the code at https://github.com/robksawyer/durandal-todo/tree/v1.0. Thanks for the response. – Rob Sawyer May 16 '13 at 05:47
0

My issue seems to be a misinterpretation of the console message and how Knockout works. You can see an explanation over at Durandal TodoMVC - Cannot write a value to a ko.computed.

Community
  • 1
  • 1
Rob Sawyer
  • 2,163
  • 1
  • 24
  • 25