javascript - Rendering in backbone error -
i'm trying render don't know javascript , compare error: uncaught referenceerror: wrapper not defined. render result of collection's fetch in same view.
var homeview = backbone.view.extend({ template: handlebars.compile(template), events: { }, initialize: function() { console.log("inhomeview"); var amici = new usercollection(); amici.fetch({ success: function() { amici.each(function(object) { console.log(object.tojson()); var wrapper=object.tojson(); }); }, error: function(amici, error) { // collection not retrieved. } }); this.render(); }, render: function() { var context=wrapper; var html =this.template(context); this.$el.html(html); return this; } }); return homeview; });
perhaps want similar (please read comments in code below):
var homeview = backbone.view.extend({ template: handlebars.compile(template), initialize: function() { // better pass collection view , listen 'reset' event this.collection.on('reset', this.render, this) }, render: function() { // convert collection json , pass template "users" var html = this.template({users: this.collection.tojson()}); this.$el.html(html); return this; } }); // how should using later in code: // create collection , pass in home view var users = new usercollection(), homeview = new homeview({collection: users, el: '#pagina'}); // fetch collection, trigger 'reset' event // view render users.fetch();
Comments
Post a Comment