Knockout JS failed to update observableArray

Lexx

So I'm trying to add content to an observable array, but it doesn't update. The problem is not the first level content, but the sub array. It's a small comments section. Basically I've this function to declare the comments

function comment(id, name, date, comment) {
    var self = this;
    self.id = id;
    self.name = ko.observable(name);
    self.date = ko.observable(date);
    self.comment = ko.observable(comment);
    self.subcomments = ko.observable([]);
}

I've a function to retrieve the object by the id field

function getCommentByID(id) {
    var comment = ko.utils.arrayFirst(self.comments(), function (comment) {
        return comment.id === id;
    });
    return comment;
}

This is where I display my comments

<ul style="padding-left: 0px;" data-bind="foreach: comments">
    <li style="display: block;">
        <span data-bind="text: name"></span>
        <br>
        <span data-bind="text: date"></span>
        <br>
        <span data-bind="text: comment"></span>
        <div style="margin-left:40px;">
            <ul data-bind="foreach: subcomments">
                <li style="display: block;">
                    <span data-bind="text: name"></span>
                    <br>
                    <span data-bind="text: date"></span>
                    <br>
                    <span data-bind="text: comment"></span>
                </li>
            </ul>
            <textarea class="comment" placeholder="comment..." data-bind="event: {keypress: $parent.onEnterSubComment}, attr: {'data-id': id }"></textarea>
        </div>
    </li>
</ul>

And onEnterSubComment is the problematic event form

self.onEnterSubComment = function (data, event) {
    if (event.keyCode === 13) {
        var id = event.target.getAttribute("data-id");
        var obj = getCommentByID(parseInt(id));
        var newSubComment = new comment(0, self.currentUser, new Date(), event.target.value);
        obj.subcomments().push(newSubComment);
        event.target.value = "";
    }
    return true;
};

It's interesting, because when I try the same operation during initialization(outside of any function) it works fine

var subcomment = new comment(self.commentID, "name1", new Date(), "subcomment goes in here");
self.comments.push(new comment(self.commentID, "name2", new Date(), "some comment goes here"));
obj = getCommentByID(self.commentID);
obj.subcomments().push(subcomment);

If anyone can help me with this, cause I'm kind of stuck :(

JotaBe

You need to make two changes:

1st, you have to declare an observable array:

self.subcomments = ko.observableArray([]);

2nd, you have to use the observable array methods, instead of the array methods. I.e. if you do so:

obj.subcomments().push(subcomment);

If subcomments were declared as array, you'd be using the .push method of Array. But, what you must do so that the observable array detect changes is to use the observableArray methods. I.e, do it like this:

obj.subcomments.push(subcomment);

Please, see this part of observableArray documentation: Manipulating an observableArray:

observableArray exposes a familiar set of functions for modifying the contents of the array and notifying listeners.

All of these functions are equivalent to running the native JavaScript array functions on the underlying array, and then notifying listeners about the change

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update ObservableArray - Knockout.js

From Dev

Knockout: Update item in an observableArray

From Dev

knockout.js: No concat on observableArray

From Dev

Custom filtering knockout js observableArray

From Dev

Knockout.js contenteditable observablearray

From Dev

knockout.js: No concat on observableArray

From Dev

Knockout js observableArray is not getting updated

From Dev

knockout observableArray elements don't update view

From Dev

Knockout update observableArray item does not reflect in UI?

From Dev

Knockout.js - Registering to change in observableArray

From Dev

Knockout JS - Modifying properties of items in observableArray

From Dev

Knockout.js - Registering to change in observableArray

From Dev

knockout.js observableArray computed value

From Dev

knockout.js observableArray changes not reflecting in UI

From Dev

Sorting observableArray with added item Knockout JS

From Dev

Knockout does not update UI when observableArray is a property of other model

From Dev

My Observablearray in knockout foreach binding doesn't update?

From Dev

My Observablearray in knockout foreach binding doesn't update?

From Dev

Knockout ObservableArray.replace() function does not update 'optionsText' binding

From Dev

Can not find index form observablearray knockout.js

From Dev

knockout.js validation on observableArray to require all items (no empty fields)

From Dev

Knockout not create and observableArray

From Dev

Filtering an observableArray in Knockout

From Dev

Knockout observableArray is not binding correctly

From Dev

Knockout observableArray push not working

From Dev

Load Knockout observableArray not working

From Dev

Knockout observableArray not updating UI

From Dev

Update an array item in knockout js

From Dev

Knockout move observableArray item down

Related Related

  1. 1

    Update ObservableArray - Knockout.js

  2. 2

    Knockout: Update item in an observableArray

  3. 3

    knockout.js: No concat on observableArray

  4. 4

    Custom filtering knockout js observableArray

  5. 5

    Knockout.js contenteditable observablearray

  6. 6

    knockout.js: No concat on observableArray

  7. 7

    Knockout js observableArray is not getting updated

  8. 8

    knockout observableArray elements don't update view

  9. 9

    Knockout update observableArray item does not reflect in UI?

  10. 10

    Knockout.js - Registering to change in observableArray

  11. 11

    Knockout JS - Modifying properties of items in observableArray

  12. 12

    Knockout.js - Registering to change in observableArray

  13. 13

    knockout.js observableArray computed value

  14. 14

    knockout.js observableArray changes not reflecting in UI

  15. 15

    Sorting observableArray with added item Knockout JS

  16. 16

    Knockout does not update UI when observableArray is a property of other model

  17. 17

    My Observablearray in knockout foreach binding doesn't update?

  18. 18

    My Observablearray in knockout foreach binding doesn't update?

  19. 19

    Knockout ObservableArray.replace() function does not update 'optionsText' binding

  20. 20

    Can not find index form observablearray knockout.js

  21. 21

    knockout.js validation on observableArray to require all items (no empty fields)

  22. 22

    Knockout not create and observableArray

  23. 23

    Filtering an observableArray in Knockout

  24. 24

    Knockout observableArray is not binding correctly

  25. 25

    Knockout observableArray push not working

  26. 26

    Load Knockout observableArray not working

  27. 27

    Knockout observableArray not updating UI

  28. 28

    Update an array item in knockout js

  29. 29

    Knockout move observableArray item down

HotTag

Archive