javascript - What's the proper way to apply ko.computeds (Knockout computeds) to a Durandal viewmodel? -
i'm new durandal , haven't had luck applying ko.computed methods inside of viewmodel. can tell me correct syntax or pattern is?
you can see entire project @ https://github.com/robksawyer/durandal-todo/blob/master/views/todos.html.
each of computeds apply gets following error during bind.
error("cannot write value ko.computed unless specify 'write' option. if wish read current value, don't pass parameters.")
compose method requesting viewmodel , view:
<!--ko compose: { model: router.activeitem, //wiring router aftercompose: router.aftercompose, //wiring router cacheviews: false, //telling composition keep views in dom, , reuse them (only idea singleton view models) transition: 'fadein' }--><!--/ko-->
viewmodel:
// count of 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
defining vm singleton first , adding ko.computed methods take care of error message.
var vm = { current : current, todos: todos, ... // remove ko.computeds singleton }; vm.completedcount = ko.computed(function () { return ko.utils.arrayfilter(todos(), function (todo) { return todo.completed(); }).length; }, vm); // add other ko.computeds return vm;
Comments
Post a Comment