Durandal,Knockout 绑定 class 以查看 - 无输出

Durandal, Knockout binding class to view - no output

视图模型

define([
'services/blogrepository',
'plugins/router'], function (blogrepository, router) {

 var Blog = function(data) {
    this.ID = ko.observable(data.BID);
    this.Titel = ko.observable(data.BTitel);
    this.Content = ko.observable(data.BContent);
    this.Time = ko.observable(data.BTimestamp);
    }

var viewmodel = function () {
    this.router = router;
    this.blogrepository = blogrepository;
    this.blogentries = ko.observableArray([]);

    this.blogrepository.getAllBlog()
                    .then(function (result) {

                         this.blogentries = $.map(result, function (item) { return new Blog(item) });

                    });


    this.activate = function () {
    };

    this.attached = function (view, parent) {

    };

};

return viewmodel;});

查看

<article data-bind="foreach: blogentries">
    <span data-bind="text: Titel"> </span>
</article>

大家好,

我无法在视图中显示博客条目。我在这里没有看到什么?

getAllBlog() 很好,我得到了数据,如果我 console.log 他告诉我的 blogentries:

[Blog, Blog]
0: Blog
Content: c()
ID: c()
Time: c()
Titel: c()
__proto__: Blog
1: Blog
Content: c()
ID: c()
Time: c()
Titel: c()
__proto__: Blog

非常感谢,祝你星期天愉快。

克里斯

this.blogentries = $.map(...);

您正在分配给 blogentries 而不是设置其内容。这会丢弃它的 observableArray。相反,

this.blogentries($.map(...));

更新:您还有一个this问题。在函数内,this 的值与函数外的值不同。最好使用行

var self = this;

作为任何构造函数的第一行。在构造函数中,使用 self 而不是 this 这样您就可以始终知道您引用的是什么。这是一个很常见的陷阱。您还应该考虑是否真的需要一个构造函数,或者只是一个 returns 对象的函数。