0

当我单击按钮 1 时,我得到具有 50 个联系人数组的对象(包含具有电话号码、地址的数组集合......),然后当我单击按钮 2 时,我得到相同的对象,但我的第一个对象被删除,而我想显示50 + 50 = 100 个联系人数组。我尝试了 concat 方法,但实现起来有些困难。

   viewModel.initializeListener = function() {

    $('#button1').click(function() {
        document.getElementById("button2").style.visibility = "hidden";
        $('#retrievedContactsDiv').html('');
         nbDisplayedContacts = 0;
        console.info("test");
         viewModel.ui.FlashbackReport.MoreContacts();



    });

    $('#button2').click(function() {
        viewModel.ui.FlashbackReport.MoreContacts();
        console.info("test");
    });

    }; `


  viewModel.WeHaveMoreContacts = function(data) {
    console.info("test:", data)
    if (viewModel.MoreContacts) {

        var newArray=ko.mapping.fromJS(data, viewModel.MoreContacts);
           var concatenated = newArray.concat(dataArray);
           viewModel.MoreContacts.contacts(concatenated);


    } else {
        viewModel.MoreContacts = ko.mapping.fromJS(data);
        var dataArray = viewModel.MoreContacts.contacts();

    }

我有一个参数,其中包含要为服务器跳过的联系人数量。

调用服务器的函数然后调用映射函数:

viewModel.ui.FlashbackReport.MoreContacts()

问题:对象#没有方法'concat'

4

1 回答 1

1

I made a fiddle that may help you.

The first part of the function generates new contacts and the second one add them to the existing contacts.

var VM = function () {
    var self = this;
    self.contacts = ko.observableArray();

    self.addMore = function () {
        // simulate server response 
        var offset = self.contacts().length;
        var dataFromServer = [];
        for (var index = 0; index < 10; index++) {
            dataFromServer.push({
                name: 'contact ' + offset + index
            });
        }
        // add each new item to existing items.
        ko.utils.arrayForEach(dataFromServer, function (item) {
            self.contacts.push(item);
        });

    };
}

Feel free to ask more explanation.

I hope it helps.

于 2013-07-24T11:25:19.487 回答