javascript - 我的 View 没有从我的 Controller 接收数据

标签 javascript jquery backbone.js

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

我的 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." });
        }
    });
},

我的观点:

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为:undefined 在 Controller 中,var document 有效。

为什么YyY?如何在 View 中访问 Controller 发送的 json?

谢谢

最佳答案

console.log(this.documents);

this.documents = this.options.documents;

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

更新:

new App.Views.Index({ 集合: 文档 });

this.documents = this.options.documents;

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

关于javascript - 我的 View 没有从我的 Controller 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7967152/

相关文章:

javascript - 如何使用 HTML5/Javascript 下载并显示文本文件?

javascript - 我怎样才能修改我的 jQuery 函数来更新数字计数器

javascript - Jquery:如何在ajax中添加重复数据

backbone.js - 带有文本的 require.js backbone.js 的正确构建步骤!插入

django - Backbone js 与 django 一起使用有什么优势

javascript - 单页应用程序安全问题

javascript - 输入后清除 HTML 输入并防止换行

javascript - Angular 选择不起作用

javascript - 如何将对象数组放入新的 Json 对象中

jquery - jQuery 中的实时搜索