4

我有这个代码:

<ul id="chats" 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>
      <a class="read_more_messages" data-bind="visible: moreChats, click: showMoreChats">Read More Messages...</a>
   </li>
</ul>

function ChatListViewModel(chats) {
   // Data
   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.moreChats = ko.observable(true);

   self.showMoreChats = function () {
       var LastChatGroupNumber = self.chats()[self.chats().length - 1].ChatGroupNumber;

       $.ajax({
           url: $.CourseLogic.dataitem.GetMoreChatsUrl,
           data: "{chatGroupNumber: " + ko.toJSON(LastChatGroupNumber + 1) + "}",
           type: "post",
           contentType: "application/json",
           success: function (chats) {
               var chatList = self.chats();
               $.each(chats, function (index, chat) {
                   self.chats.push(chat);
               });
           }
       });
   }

}

ko.applyBindings(new ChatListViewModel(initialData));

但是当调用 showMoreChats 函数时出现此错误:

Unable to parse bindings. Message: TypeError: CommentList is not a function; Bindings value: visible: CommentList().length == 0

这是什么意思?

4

3 回答 3

8

并不是说 CommentList 是未定义的,只是它不是可观察的(因此不是函数)。原因是在您的 ajax 回调中,您只是“按原样”推送从服务器接收到的“聊天”对象。例如,您没有创建一个名为 CommentList 的新 observableArray,而只是放置了一个裸数组 CommentList - 因此 KO 抛出了错误。

您需要进行与在 viewmodel 构造函数中构造 self.chats 时相同的转换,例如:

$.each(chats, function(index, chat) {
    self.chats.push(
        {
            CourseItemDescription: chat.CourseItemDescription,
            CommentList: ko.observableArray(chat.CommentList),
            CourseItemID: chat.CourseItemID,
            UserName: chat.UserName,
            ChatGroupNumber: chat.ChatGroupNumber,
            ChatCount: chat.ChatCount,
            NewCommentText: ko.observable("")
        }
    );
});

顺便说一下,你还应该看看 ko.mapping 插件,它可以为你做这个转换。

编辑:此外,为了获得更好的性能,您不应将每个单独的项目推入可观察数组,而是将它们添加到一个批量操作中,例如:

self.chats( self.chats().concat( ko.utils.arrayMap(chats, function(chat) {
    return { /* ... same as before ... */ };
} ) );
于 2012-07-03T01:53:55.147 回答
3

通过谷歌找到了这个问题,添加了我的案例以供参考。这真的,真的很愚蠢;然而这是我经常犯的疏忽造成的错误:

当使用其中一个ko.utils数组函数(arrayMap、arrayForEach、arrayFirst 等)时,当您忘记指定源数组时,Knockout 将抛出此错误,例如。当你这样做时:

ko.utils.arrayMap(function(item) { ... })

代替:

ko.utils.arrayMap(myArray, function(item) { ... });
于 2015-06-16T21:21:55.130 回答
0

这意味着您的绑定无法检测到 CommentList,即 CommentList 在当前上下文中未定义。

于 2012-07-03T01:41:39.760 回答