简体   繁体   English

knockout.js observableArray不被识别为函数

[英]knockout.js observableArray is not recognized as a function

I have been using knockout.js for a while now, and haven't encountered this problem before. 我一直在使用knockout.js一段时间,之前没有遇到过这个问题。 Usually, when I try to push a new js object to an observableArray, it works without an issue, but for some reason, this time around I'm getting this error: 通常,当我尝试将新的js对象推送到observableArray时,它的工作没有问题,但出于某种原因,这次我得到了这个错误:

TypeError: self.Students.push is not a function

Here is a snippet of my code: 这是我的代码片段:

window.ApiClient = {
    ServiceUrl: "/api/students",

    Start: function () {
        var viewModel = ApiClient.ViewModel(ngon.ClientViewModel);
        ko.applyBindings(viewModel);                
        viewModel.get();
    }    
};

ApiClient.ViewModel = function(data) {
    var self = this;
    ko.mapping.fromJS(data, {}, this);    

    this.get = function (id) {
        if (id == undefined) {
            return ApiClient.Service.get(self.PageSize(), self.PageNumber(), function (data) {
                self.Students(data);
            });
        }
    }

    this.post = function () {        
        return ApiClient.Service.post(self.DetailedStudent, function (data) {                        
            self.Students.push(data);
        });
    }

    return this;
}

ApiClient.Service = function () {    
    var _get = function (pageSize, pageNumber, callback) {
        sv.shouldShowLoading = false;
        var queryParams = String.format("?pageSize={0}&pageNumber={1}", pageSize, pageNumber);        
        $.ajax(ApiClient.ServiceUrl + queryParams, {
            dataType: "json",
            type: "get",
            success: callback
        });
    }

    var _post = function (student, callback) {
        $.ajax(ApiClient.ServiceUrl, {
            data: ko.mapping.toJSON(student),
            type: "post",
            contentType: "application/json; charset-utf-8",
            statusCode: {
                201 /*Created*/: callback,
                400 /*BadRequest*/: function (jqxhr) {
                    var validationResult = $.parseJSON(jqxhr.responseText);
                    alert(jqxhr.responseText);
                }
            }
        });
    }

    return {        
        get: _get, 
        post: _post
    };
}();

$(document).ready(function () {    
    ApiClient.Start();      
});

My student object is a very simple C# object that has Id, FirstName, LastName. 我的学生对象是一个非常简单的C#对象,它有Id,FirstName,LastName。 The get() function works without any issues, it's just the callback function from the post() that cannot push the resulting data. get()函数没有任何问题,它只是post()中不能推送结果数据的回调函数。 Also, the data being returned back from the server looks correct: 此外,从服务器返回的数据看起来是正确的:

{"Id":"rea","FirstName":"asdf","MiddleName":null,"LastName":"rrr"}

I solved this! 我解决了这个! It's because the initial viewModel, when being instantiated by the page's view model object had 'null' for its Students property. 这是因为初始viewModel在被页面的视图模型对象实例化时,其Students属性为'null'。

knockout.js requires non-null values for all fields that are to be auto mapped. knockout.js要求所有要自动映射的字段的非空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM