0
votes

I'm using ko.utils.arrayFirst to get a matching item from my view model, then trying to update one of it's properties, but I'm getting a "is not a function" error. Any help would be appreciated. I'm getting the error when I call match.NewMessage(""). Shouldn't match be an instance of an item in my observable array from my view model? SendMessage is a function defined on my view model, hence the "self" references, and InstantMessages is defined as a ko.observableArray.

self.SendMessage = function (im) {
    var match = ko.utils.arrayFirst(self.InstantMessages(), function (item) {
        return im.InstantMessageId === item.InstantMessageId;
    });

    if (match) {
        var newMessage = new Chat.Message();
        newMessage.FromUser = self.User;
        newMessage.Text = match.NewMessage;
        newMessage.InstantMessageId = match.InstantMessageId;

        match.NewMessage("");

        self.ChatHub.server.sendMessage(newMessage).fail(function (e) { alert(e); });
    }
};
1

1 Answers

1
votes

Just because you've defined InstantMessages as a ko.observableArray(), it doesn't mean that all of the properties in the objects contained in the array are observable as well.

The NewMessage property needs to be initialised as a ko.observable()

var im;
im.NewMessage = ko.observable();