user1505740
user1505740

Reputation: 11

Knockout : I have to perform an action after two observable array get populated

I am new to knockout.In my viewmodel I have perform an action only when two observable arrays get populated from database. How can I achieve this?

Thanks in advance

Upvotes: 0

Views: 77

Answers (1)

Rango
Rango

Reputation: 3907

The easiest way is to define computed observable that checks length of every array and returns true if both lengths are not zeroes.

self.bothPopulated = ko.computed(function(){
    return self.array1().length > 0 && self.array2().length > 0;
});

Then just subscribe on it with appropriate action:

self.bothPopulated.subscribe(function(val){
    if (val) alert('both populated');
});

http://jsfiddle.net/Zk6Lc/

Upvotes: 2

Related Questions