Click here to Skip to main content
16,020,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a viewmodel that gets its data from a MVC controller and is converted to JSON in the view. The data is comprised of two lists (PlayList and TeamMembers). I use Knockout Mapping make the data observable.

I would like to create a function called 'getSelected' for each child viewModel (playListItems and teamMemberItems) and be able to call the method from within the view. However, my attempt seems to return the entire parent viewmodel and not the filtered child viewmodel.

Any help would be appreciated.

What I have tried:

var ViewModel = function(data) {
        var self = this;
        self.playListItems = data.PlayList;
        self.teamMemberItems = data.TeamMembers;

        self.playListItems.getSelected = ko.observable(function(id) {
            var currentItem = ko.utils.arrayFirst(self.playListItems(),
                function(item) {
                    return item.RecordingId() === id;
                }).item;

            return currentItem;
        });
    }
Posted
Updated 13-Mar-18 9:39am

1 solution

I was able to get this to work by adding '._latestValue' to the syntax.. I am not sure if it the 'correct' way to do it.

self.playListItems.getSelected = function(id) {
    var currentItem = ko.utils.arrayFirst(self.playListItems._latestValue,
        function(item) {
            return item.RecordingId() === id;
        });

    return currentItem;
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900