0

when I click on button1 I get object with 50 contacts array (containing collection of arrays with phoneNumbers, Addresses...), then when I click on button 2 I get the same object but my first object is erased whereas I would like to display 50 + 50 = 100 contacts array. I tried concat method but I have some difficulties to implement.

   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();

    }

I have a parameter with number of contacts to skip for the server.

function which call the server then call the mapping function :

viewModel.ui.FlashbackReport.MoreContacts()

Problem : Object # has no method 'concat'

2
  • Could you please post a jsfiddle showing a complete example of what you want to do? It is not clear with the code you posted. Commented Jul 24, 2013 at 9:00
  • my code is too long with jsonp requests, thx for help anyway Commented Jul 24, 2013 at 9:38

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.