Durandal KO Binding View Issue

use*_*905 2 asp.net-mvc durandal

我有Durandal viewmodel工作方式如下:

define(function (require) {
var http = require('durandal/http');

return {
    subjectItem: function (data) {
        var self = this;
        self.Subject_ID = data.Subject_ID;
        self.Subject_Name = data.Subject_Name;
    },
    subjects: ko.observableArray([]),
    activate: function () {
        var self = this;
        http.ajaxRequest("get", "/api/values/getsubjects")
            .done(function (allData) {
                var mappedSubjects = $.map(allData, function (list) { return new self.subjectItem(list); });
                self.subjects(mappedSubjects);
            });
    }
};
});
Run Code Online (Sandbox Code Playgroud)

但是,在首次导航到此视图时,它会运行ajax调用但不会更改dom,再次导航到它并且它们都会出现(仍然运行ajax调用).这是我的HTML:

<table>
<thead>
    <tr>
        <th>Name</th>
    </tr>
</thead>
<tbody data-bind="foreach: subjects">
    <tr>
        <td data-bind="text: Subject_Name"></td>
    </tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

任何线索,我对使用Durandal很新,但现在担心它不会有效地保持dom更新.

Jul*_*ste 6

我认为您的问题非常类似于:HotTowel:Viewmodel在页面之间导航时丢失映射

AJAX调用是异步任务,因此您需要在activate函数中返回一个promise,告诉durandal等待数据被检索,然后再继续应用绑定.