Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的视图未从控制器接收数据_Javascript_Jquery_Backbone.js - Fatal编程技术网

Javascript 我的视图未从控制器接收数据

Javascript 我的视图未从控制器接收数据,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我将只显示我正在使用的代码部分。如果您认为需要查看所有代码,请告诉我 我的控制器: index: function() { var documents = new App.Collections.Documents(); documents.fetch({ success: function() { new App.Views.Index({ collection: documents }); }, erro

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

我的控制器:

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);
    }
});
App.Views.Index=Backbone.View.extend({
初始化:函数(){
console.log(this.documents);
this.documents=this.options.documents;
这个。render();
},
render:function(){
如果(this.documents.length>0){
var out=“
    ”; _(本文件)。各功能(项目){ out+=“
  • ”; }); out+=“
”; }否则{ out=“没有文档!”; } $(this.el).html(out); $('#app').html(this.el); } });
文档代码的console.log为:未定义 在控制器中,var文档有效

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


谢谢,

我不太清楚。。。但是在进入初始化功能之前是否定义了
此.documents
?似乎不是这样-尝试切换线路:

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

this.documents=this.options.documents

甚至设置了在
此.documents
之前使用console.log

更新:

newapp.Views.Index({collection:documents})

this.documents=this.options.documents


是不是
this.documents=this.options.collection?因为我看不到您在任何地方传递选项.documents变量。

您传递的是
collection:documents
,因此在您的视图中,您可以使用
this.collection
访问它


查看这个显示它工作的JSFIDLE:

您的评论是书面的,但我仍然有同样的问题。此日志未定义。var documents=new App.Collections.documents();在控制器中是一个局部变量-正如我说的,我真的不知道主干,但这对我来说似乎不正确。。。查看如何在控制器和视图之间传递数据否,视图对象上的任何采集选项都会自动可用。如果我错了,请参阅@dira纠正我,但我没有看到变量
this.documents
在其他地方定义。我可以理解在扩展主干.View时如何访问它,但是使用的变量名是
collection
而不是
documents
是的,Leo,你说得对,this.collection应该工作,而不是this.documents或this.options.documents。很好的观察。