why does this knockout method receive a form element instead of the object its nested in?

Posted by ladookie on Stack Overflow See other posts from Stack Overflow or by ladookie
Published on 2012-07-03T02:49:57Z Indexed on 2012/07/03 3:15 UTC
Read the original article Hit count: 201

Filed under:
|

I have this HTML:

<ul class="chat_list" data-bind="foreach: chats">
  <li>
    <div class="chat_response" data-bind="visible: CommentList().length == 0">
      <form data-bind="submit: $root.addComment">
        <input class="comment_field" placeholder="Comment…" 
          data-bind="value: NewCommentText" 
        />
      </form>
    </div>            
  </li>
</ul>

and this JavaScript:

function ChatListViewModel(chats) {

   // var self = this;

    self.chats = ko.observableArray(ko.utils.arrayMap(chats, function (chat) {
        return { CourseItemDescription: chat.CourseItemDescription,
            CommentList: ko.observableArray(chat.CommentList),
            CourseItemID: chat.CourseItemID,
            UserName: chat.UserName,
            ChatGroupNumber: chat.ChatGroupNumber,
            ChatCount: chat.ChatCount,
            NewCommentText: ko.observable("")
        };
    }));

    self.newChatText = ko.observable();

    self.addComment = function (chat) {
        var newComment = { CourseItemDescription: chat.NewCommentText(),
            ParentCourseItemID: chat.CourseItemID,
            CourseID: $.CourseLogic.dataitem.CourseID,
            AccountID: $.CourseLogic.dataitem.AccountID,
            SystemObjectID: $.CourseLogic.dataitem.CommentSystemObjectID,
            SystemObjectName: "Comments",
            UserName: chat.UserName
        };
        chat.CommentList.push(newComment);
        chat.NewCommentText("");
    };
}
ko.applyBindings(new ChatListViewModel(initialData)); 

When I go into the debugger it shows that the chat parameter of the addComment() function is a form element instead of a chat object.

Why is this happening?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about knockout.js