Durandal TodoMVC - 无法为ko.computed写一个值 [英] Durandal TodoMVC - Cannot write a value to a ko.computed
问题描述
我正在尝试使用:
function dependentObservable(){
if(arguments.length> ; 0){
if(typeof writeFunction ===function){
//写一个值
writeFunction.apply(evaluateatorFunctionTarget,arguments);
} else {
throw new Error(除非你指定一个'write'选项,否则不能写一个值到ko.computed,如果你想读取当前值,不要传递任何参数。 );
}
返回此; //允许链接赋值
} else {
//读取值
if(!_hasBeenEvaluated)
evaluateImmediate();
ko.dependencyDetection.registerDependency(dependentObservable);
return _latestValue;
}
}
您在控制台中看到的是精简
如果你想看到属性返回的值,你必须调用它。
I'm trying to build a version of the todo app using Durandal (including Knockout + RequireJS) from a TodoMVC template. I realize that a todo app doesn't really show off the features of Durandal, but I'm on the learning path and figured it would be a good first project.
Anyway, in the process I've stumbled upon an error that I'm unable to solve (see below).
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.")
I've also attached an image that shows these in the console.
You can find the source code at https://github.com/robksawyer/durandal-todo. The todo viewmodel is located at https://github.com/robksawyer/durandal-todo/blob/master/viewmodels/todos.js.
Update: Most of the Knockout code is borrowed from the Knockout+Require TodoMVC project at https://github.com/tastejs/todomvc/tree/gh-pages/labs/dependency-examples/knockoutjs_require/
Thanks for your time.
I think you're misreading the console.
For example, "allCompleted" is a property on your view model, which is declared as a dependent observable (i.e. a "computed"):
// writeable computed observable to handle marking all complete/incomplete
self.allCompleted = ko.computed({
// -- trimmed --
});
What you're seeing in the console isn't the Cannot write a value
error; it's the debug output for a computed
property - i.e. its function definition. For reference, here's the function definition of a dependent observable straight from the knockout (2.2.1) source:
function dependentObservable() {
if (arguments.length > 0) {
if (typeof writeFunction === "function") {
// Writing a value
writeFunction.apply(evaluatorFunctionTarget, arguments);
} else {
throw new 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.");
}
return this; // Permits chained assignments
} else {
// Reading the value
if (!_hasBeenEvaluated)
evaluateImmediate();
ko.dependencyDetection.registerDependency(dependentObservable);
return _latestValue;
}
}
What you're seeing in your console is the minified version of this code.
If you want to see the value that's returned by the property you'd have to invoke it.
这篇关于Durandal TodoMVC - 无法为ko.computed写一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!