我的视图没有从我的控制器接收数据

发布于 2024-12-13 06:03:18 字数 1333 浏览 0 评论 0原文

我将只展示我有问题的代码部分。如果您认为需要查看所有代码,请告诉我。

我的控制器:

index: function() {
    var documents = new App.Collections.Documents();
    documents.fetch({
        success: function() {
            new App.Views.Index({ collection: documents });
        },
        error: function() {
            new Error({ message: "Error loading documents." });
        }
    });
},

我的视图:

App.Views.Index = Backbone.View.extend({
    initialize: function() {
        console.log(this.documents);
        this.documents = this.options.documents;
        this.render();
    },

    render: function() {
        if(this.documents.length > 0) {
            var out = "<h3><a href='#new'>Create New</a></h3><ul>";
            _(this.documents).each(function(item) {
                out += "<li><a href='#documents/" + item.id + "'>" + item.escape('title') + "</a></li>";
            });
            out += "</ul>";
        } else {
            out = "<h3>No documents! <a href='#new'>Create one</a></h3>";
        }
        $(this.el).html(out);
        $('#app').html(this.el);
    }
});

文档代码的console.log为:未定义 在控制器中,var document 是有效的。

为什么YYY?如何在视图中访问控制器发送的 json?

谢谢,

I will just show the part of the code I am having problens. If you think that you need to see all the code, let me know.

My Controller:

index: function() {
    var documents = new App.Collections.Documents();
    documents.fetch({
        success: function() {
            new App.Views.Index({ collection: documents });
        },
        error: function() {
            new Error({ message: "Error loading documents." });
        }
    });
},

My View:

App.Views.Index = Backbone.View.extend({
    initialize: function() {
        console.log(this.documents);
        this.documents = this.options.documents;
        this.render();
    },

    render: function() {
        if(this.documents.length > 0) {
            var out = "<h3><a href='#new'>Create New</a></h3><ul>";
            _(this.documents).each(function(item) {
                out += "<li><a href='#documents/" + item.id + "'>" + item.escape('title') + "</a></li>";
            });
            out += "</ul>";
        } else {
            out = "<h3>No documents! <a href='#new'>Create one</a></h3>";
        }
        $(this.el).html(out);
        $('#app').html(this.el);
    }
});

The console.log of document code is: undefined
In the controller, var document is valid.

WHYYyY? How I can Access the json sent by the Controller in the view?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

比忠 2024-12-20 06:03:18

console.log(this.documents);

this.documents = this.options.documents;

您在 this.documents 之前使用 console.log 是甚至设置。

更新:

new App.Views.Index({ collection: Documents });

this.documents = this.options.documents;

难道不是 this.文档 = this.options.collection; ?因为我没有看到您在任何地方传递 option.documents 变量。

console.log(this.documents);

this.documents = this.options.documents;

Your using console.log before this.documents is even set.

UPDATE:

new App.Views.Index({ collection: documents });

this.documents = this.options.documents;

Wouldn't it be this.documents = this.options.collection; ? Because I don't see you passing the option.documents variable anywhere.

弥枳 2024-12-20 06:03:18

您传递collection:documents,因此在您看来,您可以使用this.collection访问它。

看看这个jsfiddle,它显示了它的工作原理: http://jsfiddle.net/dira/TZZnA/

You pass collection: documents, so in your view you access it with this.collection.

Check out this jsfiddle that shows it working: http://jsfiddle.net/dira/TZZnA/

尾戒 2024-12-20 06:03:18

我不太了解主干...但是在进入初始化函数之前定义了 this.documents 吗?似乎不是 - 尝试切换线路:

this.documents = this.options.documents; // define the variable
console.log(this.documents); // console log it 

I dont know backbone too well ... but is this.documents defined before entering the initialize function ? seems not to be - try switching around the lines :

this.documents = this.options.documents; // define the variable
console.log(this.documents); // console log it 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文