我将只显示我有问题的代码部分。如果您认为需要查看所有代码,请告诉我。
我的 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/