简体   繁体   English

将数据添加到ko.observablearray的末尾

[英]Add data to end of ko.observablearray

I'm trying to add data to the end of an observable array but it's just not working as expected. 我正在尝试将数据添加到可观察数组的末尾,但它只是没有按预期工作。 I bet it is something minor but I just can't get my head around it. 我打赌这是次要的,但我无法理解它。

What I am doing: 我在做什么:

      self.businesses = ko.observableArray();

        function Business(business) {
            var self = this;
            self.BusinessID = ko.observable(business.BusinessID );
            self.Type = ko.observable(business.Type);
            self.Location = ko.observable(business.Location);
        }

    /*ajax get array of businesses as follows:
 [
        {
            "$id": "1",
            "BusinessID ": 62,
            "Type": "Data",
            "Location": "Data"
            },
        {
            "$id": "2",
            "BusinessID ": 63,
            "Type": "Data",
            "Location": "Data"
        },
        {
            "$id": "3",
            "BusinessID ": 64,
            "Type": "Data",
            "Location": "Data",      
        } ]
    */

                var mappedBusinesses = $.map(data, function (business) { return new Business(business) });
            self.businesses(mappedBusinesses);

This all works as expected and the obersablearray is populated. 这一切都按预期工作,并且填充了obersablearray。

However if I go to add another business, it wont work. 但是,如果我去添加另一项业务,它将无法工作。 For example, if I call the ajax that returns this (as newBusiness): 例如,如果我调用返回此的ajax(作为newBusiness):

{
    "$id": "1",
    "BusinessID ": 68,
    "Type": "Data",
    "Location": "Data"
}

and I do: 我这样做:

self.businesses().push(newBusiness);

It adds to the array as an "Object" not a Business. 它将数组添加为“对象”而非业务。 So I thought I would do: 所以我想我会这样做:

var bus = $.map(newBusiness, function (business) { return new Business(business) });
self.businesses().push(bus);

But I get the error in the JS console "Uncaught TypeError: Cannot read property 'BusinessID' of null 但是我在JS控制台中遇到错误“Uncaught TypeError:无法读取null的属性'BusinessID'

So I made a new var and added the brackets: [] in and it adds to the observable array but not as a "Business" object but rather as an "Array[1]" object at the end and this doesn't function as per the others. 所以我创建了一个新的var并添加了括号:[] in并且它添加到了可观察数组但不是作为“Business”对象而是作为最后的“Array [1]”对象,这不起作用其他人。 Code as follows: 代码如下:

    var newBus = {
            BusinessID: newBusiness.BusinessID,
            Type: newBusiness.Type,
            Location: newBusiness.Location               
}

        var bus = $.map(newBus, function (business) { return new Business(business) });
    self.businesses().push(bus);

As mentioned this adds to the observable array but doesn't actually add as a "business" object but rather as an "array[1]" object. 如上所述,这会添加到可观察数组,但实际上不会添加为“业务”对象,而是添加为“array [1]”对象。

I bet it's something so basic but just can't get it working! 我敢打赌它是如此基本但却无法让它发挥作用!

Argh I knew it would be simple! 唉,我知道这很简单!

It was posting the whole array to the ObservableArray...not just the object. 它将整个数组发布到ObservableArray ......而不仅仅是对象。

The fix: 修复:

self.businesses.push(newBusiness[0])

Had to add the [0] in to get it to push the actual data into the array, not the object! 不得不添加[0]以使其将实际数据推送到数组中,而不是对象!

Thanks for the answers! 谢谢你的回答!

You're evaluating the array with your push: 您正在使用推送评估数组:

self.businesses().push(newBusiness);

Observable Arrays have their own array functions, you should just do this (no parens): Observable Arrays有自己的数组函数,你应该这样做(没有parens):

self.businesses.push(newBusiness);

See this page: http://knockoutjs.com/documentation/observableArrays.html 请参阅此页面: http//knockoutjs.com/documentation/observableArrays.html

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

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