1

嗨,我的 js 文件中有以下代码。我在使用 REST api 的文件上传应用程序中使用 drundal 框架。因为http://localhost:49589/api/files它将给出存储在 DB 中的文件列表。

var Value = ko.observableArray([]);
            self.js = $.getJSON("http://localhost:49589/api/files", function (result) {
                self.Value(result);

            });

并且视图具有以下代码

<table  data-bind="foreach: Value">
             <tr>
                 <td data-bind=" text: ID "></td>

             <td data-bind=" text: Name "> </td>
             <td data-bind=" text: Type "> </td>
            <td data-bind=" text: Size "> </td>

             </tr>


        </table>

一旦我上传了一个文件,我就会收到成功警报并将其添加到数据库中。但是如果我导航到主页,则不包括新添加的文件详细信息。但是如果我关闭我的应用程序并重新启动它,我会得到新添加的文件信息。我知道会发生这种情况,因为当我导航到主页时,http://localhost:49589/api/files它没有被调用。有没有办法在导航时重新呈现主页

4

1 回答 1

2

确保您在激活函数中调用您的 API。

define(['plugins/http', 'durandal/app', 'knockout'], function (http, app, ko) {
return {
    Value: ko.observableArray([]),
    activate: function () {
        var that = this;
        return http.get('http://yourservice.com', { format: 'json' }, 'jsoncallback').then(function (response) {
            that.Value= response;
        });
    }
}
});
于 2013-09-24T23:23:40.000 回答