【问题标题】:Knockout JS: Value passed to Writeable Computed returns [object Object]?淘汰赛JS:传递给Writeable Computed的值返回[object Object]?
【发布时间】:2014-05-29 08:08:27
【问题描述】:

背景

在处理一个正在进行的学习项目时,我注意到我需要一个可写的计算函数来解决我面临的问题。这是瘦身:我正在尝试 1) 从成绩簿中删除用户指定数量的作业分数 2) 用户指定类型的。

用户输入上面的值,然后点击一个按钮,dropLowestScores。然后代码将最低分数添加到一个名为“lowest”的数组中,该数组存在于每个student 对象上。 然后,每个学生的可观察到的means 会根据新值进行更新,删除最低值。

问题

当我尝试下降时,我的问题就出现了。我不确定我的可写计算结构是否正确,但我也不确定出了什么问题。我注意到属性 'n' 和 workType 在我的 read 函数中是正确给出的,但在我的 write 中,它们并不符合预期。例如,在read 中,workType 返回默认值homework,但在我的write 函数中,它返回[object Object]

如果您能澄清我的问题并为我提供正确编写可写计算脚本的策略,我们将不胜感激。

后面是相关的JS和HTML sn-ps。

JSBinFull Project

JS

this.dropLowestScores = ko.computed({

    // read the parameters necessary to write values to property 'lowest'

    read: function() {

        // user sets value of 'n' in the page via ko 'options'

        var n = _this.n().n;

        // user selects 'workType' from list of ko options            

        var workType = _this.workType().workType;
        console.log("workType:" + workType);
        return n,workType;   
    },

    // now use current parameter values to set new values to 'lowest' arrays 

    write: function(n,workType) {
        //this.n = n;
        //this.workType = workType;

        // 'lowest' exists as a property for each student, 
        // ergo, I loop through each student 

        ko.utils.arrayForEach(_this.students(), function(student){
            var i = _this.students.indexOf(student);

            console.log("_this.assignments: " + _this.assignments()[i].workType().workType);
            console.log("this.workType: " + this.workType);
            console.log(_this.assignments()[i].workType().workType == this.workType);

            // if the current assignment is the same as the user-specified assignment, 
            //add the score for that assignment to array 'tmp'; then set lowest = tmp 

            if(_this.assignments()[i].workType().workType == this.workType){
                var tmp = student.scores().sort(_this.comparator).slice(0,this.n);
                console.log(tmp.length);
                student.lowest(tmp);
            }    
        });
    }
});     

HTML

<button data-bind="click: dropLowestScores">Drop Lowest Scores</button>

目前,我只是将上述函数绑定到一个按钮。理想情况下,这就是我离开它的方式。引用的其他属性,例如nworkTypemean,在表格中输入。

【问题讨论】:

    标签: javascript html knockout.js ko.observablearray


    【解决方案1】:

    我现在觉得很傻,但事实证明我走在了正确的轨道上;事实上,解决方案非常简单。在read 函数中,我只需要按以下方式定义属性:this.variable = ... 而不是var variable = ...

    this.dropLowestScores = ko.computed({
    
        // read the parameters necessary to write values to property 'lowest'
    
        read: function() {
    
            // user sets value of 'n' in the page via ko 'options'
    
            this.n = _this.n().n;
    
            // user selects 'workType' from list of ko options            
    
            this.workType = _this.workType().workType;
    
            console.log("workType:" + workType);
            return n,workType;   
        },
    
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      相关资源
      最近更新 更多