Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

computed observableArray does not update UI

I have a main ko.observableArray and i have created three computed observable arrays from it and bound the three arrays to the UI. Code is

self.ActiveVisitsList = ko.observableArray();

self.FVL = ko.computed(function () {
    return ko.utils.arrayFilter(self.ActiveVisitsList(), function (o) {
        return o.CsrID == 0;
    });
}, self);

self.MVL = ko.computed(function () {
    return ko.utils.arrayFilter(self.ActiveVisitsList(), function (o) {
        return o.CsrID == self.Me().ID;
    });
}, self);

self.OVL = ko.computed(function ()
{
    return ko.utils.arrayFilter(self.ActiveVisitsList(), function (o) {
        return (o.CsrID != self.Me().ID && o.CsrID != 0);
    });
}, self);

After some time, an object inside the main observableArray "ActiveVisitorsList", its CsrID changes from 0 to some other value but the bound UIs don't reflect. Is there any way I can manually rebind the UI with models or something to make it work?

Thanks, Aadil.

like image 631
Aadil Avatar asked Sep 24 '26 23:09

Aadil


1 Answers

If you define CsrID as observable knockout will automatically update UI. In this you have to modify your computed to this:

self.FVL = ko.computed(function () {
    return ko.utils.arrayFilter(self.ActiveVisitsList(), function (o) {
        return o.CsrID() == 0;
    });
}, self);

self.MVL = ko.computed(function () {
    return ko.utils.arrayFilter(self.ActiveVisitsList(), function (o) {
        return o.CsrID() == self.Me().ID;
    });
}, self);

self.OVL = ko.computed(function ()
{
    return ko.utils.arrayFilter(self.ActiveVisitsList(), function (o) {
        return (o.CsrID() != self.Me().ID && o.CsrID != 0);
    });
}, self);
like image 192
Artem Vyshniakov Avatar answered Oct 01 '26 01:10

Artem Vyshniakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!