【问题标题】:durandal with knockout not working淘汰赛的杜兰达尔不起作用
【发布时间】:2013-11-16 09:12:48
【问题描述】:

我有问题。我将 durandal 模板与 knockout.js 一起使用。当我尝试将数据从我的视图模型传递到视图时,没有任何反应。我看到永久加载。

这是我的视图模型:

define(['knockout', 'common/urlconfig', 'plugins/http', 'plugins/router', 'common/userService', 'durandal/app'],
    function(ko, urlconfig, http, router, userService, app) {
        "use strict";

        function article(data) {
            this.Id = ko.observable(data.Id);
            this.Description = ko.observable(data.Description);
            this.Author = ko.observable(data.Author);
            this.Tags = ko.observable(data.Tags);
        }
       // ko.applyBindings(appViewArticles);
        return {
            articles: articles,
            displayName: 'Articles viewer',
            showMessage: app.showMessage('it\'s works!'),
            Id: Id,
            Description: Description,
            Author: Author,
            Tags: Tags,

            activate: function () {
                var self = this;
                self.articles = ko.observableArray([]);
                var promise = $.getJSON("/api/article", function (allData) {
                    var mappedArticles = $.map(allData, function (i) { return new article(i) });
                    self.articles(mappedArticles);
                });
                return promise;
            }
        };

    });

我的看法:

<div class="container-fluid">
    <div class="row-fluid">
        <h2 data-bind="text: 'Welcome, ' + userName() + '!'"></h2>
    </div>
    <table>
        <thead>
            <tr><td>ID</td><td>Description</td><td>Author</td><td>Tags</td></tr>
        </thead>
        <tbody data-bind="foreach: articles">
            <tr>
                <td data-bind="text: Id"></td>
                <td data-bind="text: Description"></td>
                <td data-bind="text: Author"></td>
                <td data-bind="text: Tags"></td>
            </tr> 
        </tbody>
    </table>
</div>

怎么了?

【问题讨论】:

  • 控制台是否有任何错误?
  • 您的 viewModel 中是否有 userName 可观察道具?
  • 我的控制台没有错误

标签: knockout.js durandal


【解决方案1】:

当我像这样编辑我的 viewmodel 时:

define(['knockout', 'common/urlconfig', 'plugins/http', 'durandal/app'],
    function (ko, urlconfig, http, app) {
        var articles = ko.observableArray([]);
        http.get(urlconfig.articlesUrl).then(function(data) {
            $.each(data, function(key, item) {
                articles.push(item);
            });
        }).fail(function () {
            app.showMessage('Server error!', 'Error',['OK']);
        });
        return {
            articles: articles
        };
    });

并像这样查看

<div class="container-fluid">
    <table>
        <thead>
            <tr><td>ID</td><td>Description</td><td>Author</td><td>Tags</td></tr>
        </thead>
        <tbody data-bind="foreach: articles">
            <tr>
                <td data-bind="text: id"></td>
                <td data-bind="text: description"></td>
                <td data-bind="text: author"></td>
                <td data-bind="text: tags"></td>
            </tr> 
        </tbody>
    </table>
</div>

效果很好! =)

【讨论】:

  • 您应该知道,即使您停用视图,模块也不会被卸载。如果我没记错的话,这意味着您刚刚检索到的数据即使不使用也会保留在内存中,并且如果您重新激活视图也不会刷新。这可能是您想要的,但由于它通常不是可取的行为,我想我会指出它。仅供参考,另一种方法是将模块定义为构造函数。该函数将保持加载状态,但是当视图被停用时,从该函数创建的实例的数据可能会被垃圾收集。
【解决方案2】:

编辑:

试试

    return {
        articles: ko.observableArray([]),
        displayName: 'Articles viewer',
        showMessage: app.showMessage('it\'s works!'),


        activate: function () {
            var self = this;

            var promise = $.getJSON("/api/article", function (allData) {
                var mappedArticles = $.map(allData, function (i) { return new article(i) });
                self.articles(mappedArticles);
            });
            return promise;
        }
    };

【讨论】:

  • 我的 viewModel 中没有用户名
  • 没用。这是一个参考错误“文章未定义”
猜你喜欢
  • 2013-11-14
  • 2015-05-25
  • 2013-07-20
  • 2013-03-16
  • 2014-05-10
  • 2013-10-19
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多