0

下面的代码在我第一次调用时工作正常,但是在第二次调用中,gridviewmodel 变得混乱,所有数据都变得多余

$.ajax({
        url: 'rest service uri',
        type: 'GET',
        data: parameters,
        dataType: 'json',
        crossDomain: true,

        success: function (data) {

            function gridviewModel() {
                var self = this;
                self.items = ko.mapping.fromJS(data);
                self.columns = [
                    new column("Name", "name", "Asc"),
                    new column("URL", "name", "Asc"),
                    new column("Embargo Date", "name", "Asc"),
                    new column("Status", "name", "Asc"),
                    new column("Reviewer Name", "name", "Asc"),
                    new column("Approver Name", "name", "Asc")
                ];
            };


            ko.applyBindings(new gridviewModel());
}

我尝试使用 removeAll() 清除可观察数组,但它不适用于我。

任何想法?

只是对于任何人将来可能会遇到这个问题,我设法通过在 Knockout 映射插件中调用 ko.mapping.fromJS(data, gridviewModel.items) 来修复它完整代码是

var gridviewModel = {

        items: ko.mapping.fromJS([]),
        columns: [
                    new column("Name", "name", "Asc"),
                    new column("URL", "name", "Asc"),
                    new column("Embargo Date", "name", "Asc"),
                    new column("Status", "name", "Asc"),
                    new column("Reviewer Name", "name", "Asc"),
                    new column("Approver Name", "name", "Asc")
                ]
};

$(function () {
    ko.applyBindings(gridviewModel, document.getElementById("wip"));

});

希望这可以解决其他问题。

4

1 回答 1

0

在阅读了关于淘汰映射插件的文档后,看起来您的映射data对象应该是您的viewmode,而不是您的viewmodel. 如果我是你,我会修改我的代码以使用以下内容:

          function gridviewModel() {
                var self = this;
                self.items = data;
                self.columns = [
                    new column("Name", "name", "Asc"),
                    new column("URL", "name", "Asc"),
                    new column("Embargo Date", "name", "Asc"),
                    new column("Status", "name", "Asc"),
                    new column("Reviewer Name", "name", "Asc"),
                    new column("Approver Name", "name", "Asc")
                ];
            };


            ko.applyBindings(ko.mapping.fromJS(new gridviewModel()));

这将按照文档指定映射您的整个对象。

于 2013-09-23T00:41:45.443 回答