observableArray.removeAll() on empty array triggers update

49 views
Skip to first unread message

Fabian Sturm

unread,
Apr 13, 2013, 11:45:29 AM4/13/13
to knock...@googlegroups.com
Hi,

I just figured out that

    self.selectedConfigurations = ko.observableArray([]);
    ...
    self.selectedConfigurations.removeAll();

triggers an update of all dependant variable even thou the array was already empty.

Is this the intended behaviour?
I now will have to wrap all the removeAll calls with:

if (self.selectedConfigurations().length > 0) {
    self.selectedConfigurations.removeAll();
}

Or is there a shorter version for that?

Thanks, Fabian

Michael Best

unread,
Apr 15, 2013, 3:51:05 PM4/15/13
to knock...@googlegroups.com
You can extend observableArray by adding to ko.observableArray.fn:

ko.observableArray.fn.myRemoveAll = function() {
    if (this.peek().length > 0) {
        this.removeAll();
    }
};

-- Michael
Reply all
Reply to author
Forward
0 new messages