2

我有以下脚本(见下文)。我对此有两个问题:

1.下面这行在Knockoutjs的上下文中是什么意思?

ko.observable(null);

2.如何调用此处尚未定义的函数:

that.activePollingXhr(...

这是完整的脚本:

$(document).ready(function() {

    function ChatViewModel() {

        var that = this;

        that.userName = ko.observable('');
        that.chatContent = ko.observable('');
        that.message = ko.observable('');
        that.messageIndex = ko.observable(0);
        that.activePollingXhr = ko.observable(null);


        var keepPolling = false;

        that.joinChat = function() {
            if (that.userName().trim() != '') {
                keepPolling = true;
                pollForMessages();
            }
        }

        function pollForMessages() {
            if (!keepPolling) {
                return;
            }
            var form = $("#joinChatForm");


            that.activePollingXhr($.ajax({url: form.attr("action"), type: "GET", data: form.serialize(), cache: false,
                success: function(messages) {
                    console.log(messages);
                    for (var i = 0; i < messages.length; i++) {
                        that.chatContent(that.chatContent() + messages[i] + "\n");
                        that.messageIndex(that.messageIndex() + 1);
                    }
                },
                error: function(xhr) {
                    if (xhr.statusText != "abort" && xhr.status != 503) {
                        resetUI();
                        console.error("Unable to retrieve chat messages. Chat ended.");
                    }
                },
                complete: pollForMessages
            }));
            $('#message').focus();
        }

        that.postMessage = function() {
            if (that.message().trim() != '') {
                var form = $("#postMessageForm");
                $.ajax({url: form.attr("action"), type: "POST",
                    data: "message=[" + that.userName() + "] " + $("#postMessageForm input[name=message]").val(),
                    error: function(xhr) {
                        console.error("Error posting chat message: status=" + xhr.status + ", statusText=" + xhr.statusText);
                    }
                });
                that.message('');
            }
        }

        that.leaveChat = function() {
            that.activePollingXhr(null);
            resetUI();
            this.userName('');
        }

        function resetUI() {
            keepPolling = false;
            that.activePollingXhr(null);
            that.message('');
            that.messageIndex(0);
            that.chatContent('');
        }

    }

    //Activate knockout.js
    ko.applyBindings(new ChatViewModel());

});
4

2 回答 2

3

这只是将一个 observable 初始化null为初始值。

如果您需要调用一个可观察的函数,只需添加第二组括号。

that.activePollingXhr()()
于 2013-03-11T13:05:17.170 回答
3
  1. ko.observable(null);创建一个值为 的可观察对象null。没有什么不同ko.observable(5);,价值在哪里5

  2. 我看到您that.activePollingXhr通过将 ajax 调用的结果传递给 observable 来使用它。但是,此调用是异步的,$.ajax不会返回从服务器获取的数据,而是延迟的 jquery。您需要使用that.activePollingXhrinsudesuccess回调。这是您的代码的样子:

    $.ajax({url: form.attr("action"), type: "GET", data: form.serialize(), cache: false,
        success: function(messages) {
            console.log(messages);
            for (var i = 0; i < messages.length; i++) {
                that.chatContent(that.chatContent() + messages[i] + "\n");
                that.messageIndex(that.messageIndex() + 1);
            }
            that.activePollingXhr(messages); // <-- Note where the call to activePollingXhr is
        },
        error: function(xhr) {
            if (xhr.statusText != "abort" && xhr.status != 503) {
                resetUI();
                console.error("Unable to retrieve chat messages. Chat ended.");
            }
        },
        complete: pollForMessages
    });
    

至于您的问题下的评论:that.activePollingXhr 被定义为that.activePollingXhr = ko.observable(null);- 一个可观察的,其值为null.

于 2013-03-11T13:06:43.900 回答