Reputation: 171
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,
Upvotes: 0
Views: 61
Reputation: 4791
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/
Upvotes: 1
Reputation: 1852
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.
Upvotes: 1
Reputation: 38147
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
Upvotes: 0