我的视图没有从我的控制器接收数据
我将只展示我有问题的代码部分。如果您认为需要查看所有代码,请告诉我。
我的控制器:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.您传递
collection:documents
,因此在您看来,您可以使用this.collection
访问它。看看这个jsfiddle,它显示了它的工作原理: http://jsfiddle.net/dira/TZZnA/
You pass
collection: documents
, so in your view you access it withthis.collection
.Check out this jsfiddle that shows it working: http://jsfiddle.net/dira/TZZnA/
我不太了解主干...但是在进入初始化函数之前定义了 this.documents 吗?似乎不是 - 尝试切换线路:
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 :