2

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?

1
  • After posting this found a way: ko.utils.arrayPushAll(self.Applicants, mappedData); Commented Jan 10, 2013 at 21:21

2 Answers 2

3

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

I think you'd have to manually tell self.Applicants to do a notification of its subscribers after that.
Ahh - shouldn't call be apply? Otherwise aren't you just pushing an entire array onto the end of this array?
@AdamRackis: It's been a long day. Thanks (again)

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.