【问题标题】:Durandal, Knockout binding class to view - no outputDurandal,要查看的 Knockout 绑定类 - 无输出
【发布时间】:2015-11-09 03:28:30
【问题描述】:

视图模型

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

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

克里斯

【问题讨论】:

  • 尝试添加 Durandal 插件“Observable”。这使得 ViewModel 更容易管理,因为没有(隐藏良好的)淘汰赛。但是,您必须在 ES6 get/set 兼容的浏览器上运行。

标签: javascript arrays class knockout.js durandal


【解决方案1】:
this.blogentries = $.map(...);

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

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

更新:您还有this 的问题。在函数内,this 的值与函数外的值不同。有线路是一个好习惯

var self = this;

作为任何构造函数的第一行。在构造函数中,使用self 而不是this,这样您就始终知道您所引用的内容。这是一个非常常见的问题。您还应该考虑您是否真的需要一个构造函数,或者只是一个返回对象的函数。

【讨论】:

  • 他现在抛出这个错误:Uncaught TypeError: this.blogentries is not a function 谢谢你的帮助。
  • @ChristopherTardel 已更新。
猜你喜欢
  • 2013-08-31
  • 2013-03-11
  • 2014-12-31
  • 2014-05-31
  • 1970-01-01
  • 2013-04-16
  • 2013-03-23
  • 2013-04-30
  • 2013-09-07
相关资源
最近更新 更多