如何迭代Knockout ObservableArray? (长0)

时间:2015-12-29 13:17:55

标签: javascript jquery knockout.js asp.net-web-api knockout-3.0

我是knockout.js的新手(这也是我的第一个stackoverflow帖子),我现在面临以下问题。

我无法将来自web api的数据绑定到ko.observablearray。为什么此公告ko.observablearray的长度始终为0?该代码适用于客户端数据(通过添加新公告)..

这是JS代码:

var AnnouncementModel = function () {
    var self = this;

    self.AnnouncementText = ko.observable();
    self.AllDepartmentsBool = ko.observable();
    self.Editable = ko.observable(false);

    self.Add = function () {
        viewModel.Announcements.push(self);
        viewModel.AnnouncementToEdit(new AnnouncementModel());
    };

    self.Delete = function () {
        ajaxHelper(announcementsUri + self.ID, 'DELETE').done(
        viewModel.Announcements.remove(self));
    };

    self.Edit = function () {
        self.Editable(!self.Editable());
    };
}

//The ViewModel
function AnnouncementsViewModel() {
    var self = this;

    self.InitialData = ko.observableArray();
    self.Announcements = ko.observableArray();
    self.AnnouncementToEdit = ko.observable(new AnnouncementModel());
    self.error = ko.observable();

    function getAllAnnouncements() {
        ajaxHelper(announcementsUri, 'GET').done(function(data) {
            self.InitialData(data);        
        });
    };

    getAllAnnouncements();
};

var viewModel = new AnnouncementsViewModel();
ko.applyBindings(viewModel, document.getElementById("announcements-container"));

function createAnnouncement(announcementDto) {
    var announcement = new AnnouncementModel();
    announcement.AnnouncementText = ko.observable(announcementDto.AnnouncementText);
    announcement.AllDepartmentsBool = ko.observable(announcementDto.AllDepartmentsBool);
    announcement.Editable = ko.observable(false);

    return announcement;
}

var length = viewModel.InitialData.length;
for (i = 0; i < length; i++) {
    var newAnnouncement = createAnnouncement(InitialData[i]);
    viewModel.Announcements.push(newAnnouncement);
}

HTML:

<div id="announcements-container" style="display: inline-block; float: right">
    <ul id="announcements-list" class="newsticker" data-bind="foreach: Announcements">
        <li>
            <span data-bind="html: AnnouncementText"></span>
        </li>
    </ul>
    @Html.Partial("_AnnouncementsModal")
</div>

初始数据将从api中填充:

GOT IT WORKING! :

感谢您的快速解答。我通过使用.forEach()迭代数据来获得代码。另一个问题是initialData没有在其当前范围中填充,所以我编辑了getAnnouncements函数,就像这样工作:

function getAllAnnouncements() {
    ajaxHelper(announcementsUri, 'GET').done(function(data) {
        data.forEach(function (entry) {
            var newAnnouncement = createAnnouncement(entry);
            self.Announcements.push(newAnnouncement);
        });
    });
};

2 个答案:

答案 0 :(得分:6)

这条线可能是罪魁祸首:

var length = viewModel.InitialData.length;

请记住,InitialData功能。函数有length(它们的&#34; arity&#34;,它们有正式参数的数量),但是可观察数组的length的可观察函数不是数组length ..

你可能想要里面数组的长度:

var length = viewModel.InitialData().length;
// -------------------------------^^

您对可观察数组的push的各种调用即使length没有,因为Knockout provides push (and several other things)可观察数组函数,因为James points out

同样,这一行:

var newAnnouncement = createAnnouncement(InitialData[i]);

可能也想要使用数组(并且在viewModel.前面缺少InitialData)。

所以整个部分可能想要重构一下:

viewModel.InitialData().forEach(function(entry) {
    var newAnnouncement = createAnnouncement(entry);
    viewModel.Announcements.push(newAnnouncement);
});

或没有forEach(但实际上,它已接近2016年,并且它可以在过时的浏览器上进行调整);

var data = viewModel.InitialData();
for (var i = 0; i < data.length; ++i) {
    var newAnnouncement = createAnnouncement(data[i]);
    viewModel.Announcements.push(newAnnouncement);
}

旁注:您的代码(至少在问题中)也是The Horror of Implicit Globals的牺牲品,没有声明您在i循环中使用的for。我在上面添加了var,但这是使用forEach循环遍历数组的另一个原因。

答案 1 :(得分:0)

您还可以如下使用EcmaScript 6样式枚举:

viewModel.InitialData().forEach(item => {
    let newAnnouncement = createAnnouncement(item);
    viewModel.Announcements.push(newAnnouncement);
});