Knockout Mapping重新渲染所有内容

时间:2013-08-13 21:50:08

标签: javascript jquery ajax knockout.js knockout-mapping-plugin

我正在使用Knockout映射插件来刷新UI,每3秒从服务器检索一次JSON。 UI由一些嵌套的foreach绑定组成。但是,似乎所有foreach绑定中的所有内容都会被完全删除并在每次刷新时重新呈现,即使没有任何更改。

var testData = {
    Answers: [],
    Inspectable: {
        Categories: [{
            Id: 1,
            Name: "Test Category",
            Questions: [{
                Id: 1,
                Text: "Test Question",
                Active: true,
                Answers: [{
                    Text: "Test Answer",
                    Id: 1
                }]
            }]
        }]
    }
};

function ViewModel() {

    var self = this;

    this.refreshUrl = $("[data-view=edit]").data("url");

    this.refresh = function(callback) {
        $.get(self.refreshUrl, function(data) {
            //Ignoring actual JSON data for testing
            ko.mapping.fromJS(testData, {}, self);
            if (typeof callback == "function") {
                callback.call(self);
            }
        });
    }

    this.addedQuestion = function() {
        // Gets called for every question every refresh by afterRender
        // Never gets called at all by afterAdd
    }

};

var refreshing = false, handler;
window.viewModel = new ViewModel();

//Initialize the UI after initial AJAX is completed
viewModel.refresh(function() {

    ko.applyBindings(this);

        $(document).on("click", ".add-question", function() {
        if (!refreshing) {
            handler = setInterval(viewModel.refresh, 3000);
            refreshing = true;
        }
    });
});

有没有人看到这个明显错误?

修改

我编辑了脚本以使用静态JavaScript对象。它仍然会重新呈现每次刷新。同时更新为Knockout 2.3.0。以下是观点:

    <!-- ko foreach: Inspectable.Categories -->
        <div class="row row-fluid space-above">
            <h4 class="orange" data-bind="text: Name"></h4>
            <!-- ko foreach: { data: Questions, afterRender: $root.addedQuestion } -->
                <!-- ko if: Active() || ~$.map($root.Answers(), function(a) { return a.Id() == Id() }) -->
                    <div class="question space-above">
                        <p><strong data-bind="text: Text"></strong></p>
                        <div class="answers" data-bind="foreach: Answers">
                            <!-- ko if: $parent.AllowMultiple --><label class="checkbox"><input type="checkbox" data-url="<%= Url.Action("AddOrRemoveAnswer") %>" data-bind="attr: { value: Id, name: 'question-' + $parent.Id() }"/><!-- ko text: Text --><!-- /ko --></label><!-- /ko -->
                            <!-- ko ifnot: $parent.AllowMultiple --><label class="radio"><input type="radio" data-url="<%= Url.Action("AddOrRemoveAnswer") %>" data-bind="attr: { value: Id, name: 'question-' + $parent.Id() }"/><!-- ko text: Text --><!-- /ko --></label><!-- /ko -->
                        </div>
                    </div>
                <!-- /ko -->
            <!-- /ko -->
            <!-- ko if: Questions().length == 0 -->
                <div class="question space-above">
                    <p><strong>No questions in this category.</strong> <a class="add-question" data-bind="attr: { href: '<%= Url.Action("Create", "Questions") %>?categoryId=' + Id() + '&inProgress=true' }" target="_blank">Add some.</a> </p>
                </div>
            <!-- /ko -->
            <!-- ko if: Questions().length > 0 -->
                <div class="question space-above">
                    <a class="add-question" data-bind="text: 'New question for ' + Name(), attr: { href: '<%= Url.Action("Create", "Questions") %>?categoryId=' + Id() + '&inProgress=true' }" target="_blank"></a>
                </div>
            <!-- /ko -->
        </div>
    <!-- /ko -->

1 个答案:

答案 0 :(得分:3)

每次刷新后绑定被完全删除并重新呈现的原因,即使没有任何更改也是因为您每次刷新都会调用ko.applyBindings

您不希望刷新功能中包含ko.applyBindings。你想要一次调用ko.applyBindings就可以了。之后,KO将在数据更新时处理更新DOM(反之亦然)。你只想要:

var testDate = { ... };
function ViewModel () { ... }
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
viewModel.refresh(function () {
  // ko.applyBindings(this); <- Get rid of this here, no bueno
  $(document).on("click", ....);
});

就是这样。每次您调用refresh时,都会从更新viewModel的服务器获取数据。如果值更新,KO将根据需要更新DOM。在所有这些之后调用applyBindings时,KO会使用大刀来浏览DOM,无论是否需要更新所有绑定。


快速jQuery提示:

$.get(...)会返回promise。承诺返回有三个重要函数,.done(), .fail(), and .always().对于this.refresh()函数,返回$ .get结果,如下所示:

this.refresh = function () {
  return $.get(...);
};

然后什么叫它会这样做:

viewModel.refresh().done(function(data){
  // Callback function
});

现在您不必传递回调函数,检查回调是否是函数类型,或者担心在发生故障时处理回调函数。它也是通用的,您可以继续向一系列函数返回promise,这些函数将等待$ .get请求在执行其函数之前得到解决。

相关问题