0

here is my code:

var Feed = function (searchTerm, feeditems)
    {
        this.ID = 1;
        this.SearchTerm = searchTerm;
        this.FeedItems = ko.observableArray(feeditems);
    };

    var FeedItem = function(userName, message)
    {
        this.UserName = userName;
        this.Message = message;
    };

    var viewModel = {
        Feeds: ko.observableArray()
    };

    ko.applyBindings(viewModel);



    function getFeedData()
    {
        var url = "/Feed/GetFeedData";
        $.getJSON(url, function (data)
        {
            $.each(data, function ()
            {
                viewModel.Feeds[0].FeedItems.unshift(new FeedItem(this.UserName, this.Message));
            });
        });

        window.setInterval("getFeedData()", TIMEDELAY);
    }

I can't figure out how to update the FeedItems of the first Array element of Feeds. basically: viewModel.Feeds[0].FeedItems.unshift does not work... :(

when i just checking viewModel.Feeds, it gives me all the methods, but with viewModel.Feeds[0] it says it is undefined.

so, how to update the observable array of the first element of the observable array?

thanks!

1 Answer 1

1

To begin with viewModel.Feeds is an empty array. So Feeds[0] will be undefined. Make sure you initialize it. If you cannot, then consider creating a new object and pushing into the array, if array length is 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.