【问题标题】:Pass a function that returns the value of the ko.computed error during submit在提交期间传递一个返回 ko.computed 错误值的函数
【发布时间】:2013-04-20 02:07:52
【问题描述】:

我在提交带有淘汰赛 js 的表单时遇到问题。

我收到错误“传递一个返回 ko.computed 值的函数。”

代码如下:

 (function(records,$,undefined){
        records.models={
            student:function(data){
                var self=this;
            self.id=ko.observable(data.id);
            self.fname=ko.observable(data.fname);
            self.lname=ko.observable(data.lname);
            if(data.initial==='undefined'||data.initial===null){
                self.initial=ko.observable("");
            }else{
                self.initial=ko.observable(data.initial);
            }
            self.fullname=ko.computed(function(){
                return self.fname()+" "+" "+self.initial()+" "+self.lname();
            });
        },
        students_model:function(){
            var self=this;
            self.selectedStudent=ko.observable();
            self.students=ko.observableArray([]);
            getStudents();              

            self.save=function(){
                var form=$("#student-form");
                $.ajax({
                    type:"POST",
                    url:"/Student/create",
                    data:ko.toJSON(form[0]), //This line here is the exact point of failue
                    success:function(response){
                        records.general.handleSuccess(response);
                        if(response.status){
                            getStudents();
                        }       
                    }

                });
                return false;
            };
            function getStudents(){
                $.getJSON("/Student/data",function(result){
                    var mapped=$.map(result,function(item){
                        return new records.models.student(item);});
                    self.students(mapped);
                });
            }
        }
    };
    return records;
}(window.records=window.records||{},jQuery));

HTML

@using (Ajax.BeginForm("Create", "Student",
new AjaxOptions
{
    HttpMethod = "Post"
},
new { @class = "student-form", name = "student-form", id = "student-form" }))
{ 
<input type="text" data-bind="value:$root.fname" id="student.fname" name="student.fname" />
<input type="text" data-bind="value:$root.lname" id="student.lname" name="student.lname"/>
<input type="text" data-bind="value:$root.initial" id="student.initial" name="student.initial"/>
<input type="text" data-bind="value:$root.dob" id="dob" name="dob" />
<button data-bind="click:save">Save</button>
}

<script type="text/javascript">
ko.applyBindings(new records.models.students_model());
</script>

我在这里做错了什么?我在这里知道这个问题:Pass a function that returns the value of the ko.computed 但似乎那个人有不同的问题。以 save 方法启动时,我的代码失败。具体如下:

data:ko.toJSON(form[0])

【问题讨论】:

  • 你在哪一行得到错误?你的 html 是什么样子的?
  • 我已经更新了问题。如果您需要更多信息,请告诉我。

标签: javascript knockout.js


【解决方案1】:

ko.toJSON 期望您将 viewModel 传递给它,但您将其传递给 DOM 中的元素,因此会出现错误。

您需要将 javascript 对象(视图模型或视图模型的一部分)传递给 ko.toJSON。例如,如果您想发送学生数组,您可以这样做:

ko.toJSON(self.students);

我看到您的表单有一些输入绑定到 $root.fname$root.lname$root.initial$root.dob,但我不确定它们存在于您的视图模型中的什么位置,所以我不能告诉您究竟要通过什么。但我可以举个例子说明一种方法可以解决这个问题。

如果你有一个看起来像这样的视图模型:

var data = ...;
var vm = {
  newStudent : {
    fname : ko.observable(data.fname),
    lname: ko.observable(data.lname),
    initial: ko.observable(data.initial ?? ""),
    dob: ko.observable(data.dob)
  }
}

然后你通过调用将它绑定到你的 dom

ko.applyBindings(vm);

然后您可以像这样调用ko.toJSON

...
data:ko.toJSON(vm.newStudent),
...

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 2015-08-25
    • 2012-04-20
    • 2021-08-14
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多