how to update the array of the array of the viewModel?

71 views
Skip to first unread message

HF

unread,
Nov 26, 2012, 3:36:28 PM11/26/12
to knock...@googlegroups.com
Hi all,

here is my simple data structure:

 var Feed = function (searchTerm, feeditems)
    {
        this.ID = 1;
        this.SearchTerm = searchTerm;
        this.FeedItems = ko.observable(feeditems);        
    };
 
    var FeedItem = function(userName, message)
    {
        this.UserName = userName;
        this.Message = message;
    };
 
    var viewModel = {
        Feeds: ko.observableArray(),
        ShowRenderTimes: ko.observable(false)
    };
 
    ko.applyBindings(viewModel);



I am getting initial set of Feeds (just 1 Feed for now) from server no problem:
function getLatestFeeds()
    {
        $.getJSON('/Feed/GetLatestFeeds'function (data)
        {
            $.each(data, function ()
            {
                viewModel.Feeds.push(new Feed(this.SearchTerm, this.FeedItems));
            });
        });

    }


then I am trying to update the FeedItems for that single Feed:
function getFeedData()
    {
        var url = "/Feed/GetFeedData";
        $.getJSON(url, function (data)
        {
                $.each(data, function ()
                {
                   //HERE IS THE PROBLEM: 
                    viewModel.Feeds(0).FeedItems.push(new FeedItem(data.UserName, data.Message));
                });
        });
    }

I am getting an error:
'unable to get 'push'. object is undefined..'  So what is the exact syntax to update the FeedItems of the Feed?

I understand that the question is more javascript related than knockout, but as I am getting it while trying to make ko work, I placed my question in this forum. Thanks for any help!

Al 

shau...@gmail.com

unread,
Nov 28, 2012, 2:01:19 AM11/28/12
to knock...@googlegroups.com
Hmm, definitely doable... can you help me understand exactly what you update function is supposed to do? 

It looks maybe you are trying to replace the feeditems in the first element of the feeds observable array ...? 
If you had a jsfiddle it would help a lot to help make this work.

basil.za...@gmail.com

unread,
Dec 2, 2012, 6:11:04 AM12/2/12
to knock...@googlegroups.com
The problem here is the wrong usage of an observable array: should be viewModel.Feeds()[0], not viewModel.Feeds(0), which replaces the array with 0 instead.
Reply all
Reply to author
Forward
0 new messages