迪朗达尔KO绑定查看问题 [英] Durandal KO Binding View Issue

查看:137
本文介绍了迪朗达尔KO绑定查看问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有迪朗达尔视图模型像这样工作的:

I have Durandal viewmodel working like so:

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);
            });
    }
};
});

不过,先导航这一观点,运行Ajax调用,但不会改变DOM,再次找到它,他们都出现(仍然运行AJAX调用)。这是我的html:

However, on first navigating to this view it runs the ajax call but does not change the dom, navigate to it again and they all appear (still runs ajax call). This is my 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>

任何线索,我很新的使用迪朗达尔,但现在担心它不能有效地保持DOM更新。

Any clues, I'm very new to using Durandal but now worried it won't effectively keep the dom updated.

推荐答案

我觉得你的问题是非常相似:<一href=\"http://stackoverflow.com/questions/15298402/hottowel-viewmodel-lose-mapping-when-navigating-between-pages\">HotTowel:浏览网页之间的时候视图模型失去映射

I think your problem is very similar to: HotTowel: Viewmodel lose mapping when navigating between pages

这是AJAX调用是一个asyncronous任务,所以你需要在激活函数返回一个承诺为告诉迪朗达尔等到检索数据之前进行申请绑定。

An AJAX call is an asyncronous task so you need to return a promise in the activate function for tell to durandal to wait until the data is retrieved before to proceed to apply bindings.

这篇关于迪朗达尔KO绑定查看问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆