简体   繁体   中英

Knockout.js array append more objects

I am loading data with Ajax, for initial load, I could bind all data by using this to observableArry:

            success: function (result) {
                var mappedData = $.map(result.d, function (item) {
                    return new Applicant(item);
                });
                self.Applicants(mappedData);
            }

The question is when I want to load more to the array, I know how to add one, but what if the next load would be more than 1 object, and I want to bind to array, how could I do that?

self.Applicants.push(mappedData); won't work.

Any suggestions?

If you need to add a bunch of objects to your observable array, I would just set the whole array to its current contents, concated with the new object.

The following should work:

self.Applicants(self.Applicants().concat(mappedData));

I would use:

Array.prototype.push.apply(self.Applicants(), mappedData);
self.Applicants.valueHasMutated();

For general tips & tricks on working with observable arrays, be sure to check out Ryan Niemeyer's article on it .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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