Looping through observableArray and look for certain attributes.

3,353 views
Skip to first unread message

Dave Ackerman

unread,
May 17, 2011, 2:06:18 PM5/17/11
to knock...@googlegroups.com
I have a isSelected : ko.observable(false) which is part of my banners : ko.observableArray(). What I'm trying to accomplish is create a dependentObservable (an array?) which tracks which banners are currently selected, such that if I need to perform an action on those banners I can reference that dependentObservable and easy grab which banners I need. Is this the correct way to accomplish such functionality?

What I have tried:

In my viewModel:

selectedItems : ko.observableArray(),

And my dependentObservable:

    viewModel.selectedItems = ko.dependentObservable(function() {
        ko.utils.arrayForEach(this.banners(), function(banner) {
            return banner.isSelected();
        })
    }, viewModel);   


This isn't quite working, but I think my logic is correct? Sorry for spamming this group with so many questions!

rpn

unread,
May 17, 2011, 2:13:33 PM5/17/11
to knock...@googlegroups.com
You will want to use ko.utils.arrayFilter instead of ko.utils.arrayForEach, so like:

    viewModel.selectedItems = ko.dependentObservable(function() {
        return ko.utils.arrayFilter(this.banners(), function(banner) {

Dave Ackerman

unread,
May 17, 2011, 2:18:32 PM5/17/11
to knock...@googlegroups.com
Great. So now selectedItems() contains my selected items, how do I access their properties?

rpn

unread,
May 17, 2011, 2:23:25 PM5/17/11
to knock...@googlegroups.com
I'm not sure the exact context that you need to access the properties, but with selectedItems you can:

-bind a template to foreach through selectedItems and bind to each item's properties.
-you can loop through them yourself using a for loop or ko.utils.arrayForEach and access the properties that way
-you can directly access items in the array like viewModel.selectedItems()[0].yourProperty

If there is a different scenario that you are trying to support or if I misunderstood your question, let me know.

Dave Ackerman

unread,
May 17, 2011, 2:28:04 PM5/17/11
to knock...@googlegroups.com
Yeah, that's what I was asking -- thanks.

One thing I noticed is that when I display that contents of selectedItems() I'm getting a blank [ Object ] (s).

However, when I bind them to a div and convert them to JSON the object seems to appear just fine. What's going on there?

rpn

unread,
May 17, 2011, 2:35:55 PM5/17/11
to knock...@googlegroups.com
Not sure.  Are you talking about doing a console.log of your object?  Is this in Chrome or Firefox?

Dave Ackerman

unread,
May 17, 2011, 2:39:31 PM5/17/11
to knock...@googlegroups.com
Firefox, and yeah, console.log.

Something like:
console.log(this.selectedItems()) gives me [Object {}]

Whereas console.log(ko.toJSON(this.selectedItems())); gives me what I'd "expect"

rpn

unread,
May 17, 2011, 2:56:58 PM5/17/11
to knock...@googlegroups.com
If you click on the Object {} part, does it show you your real object?  Not sure, I am generally working in Chrome these days.
Reply all
Reply to author
Forward
0 new messages