backbone.js - Variable passed into backbone view coming up undefined -
i in process of learning backbone / underscore, , finding more break away basic stuff in tutorials, more come realize tutorials aren't teaching me of anything.
my current problem passing variable view. have 3 different templates available, render same, hoping pass template use view when being rendered collection. thought work adding property call view, , accessing this.options.property, throwing error property undefined.
i have tried number of variant options, nothing seems work. doing wrong?
thanks advance.
var projectlistview = backbone.view.extend({ el: '#projectlist', initialize: function() { this.collection = masterprojectlist; this.render(); }, render: function() { this.$el.html(""); this.collection.each(function(project) { this.renderitem(project); }, this); }, renderitem: function(project) { var projectview = new projectview({model: project, projecttype: '#theatricalprojecttemplate' }); // passing in project type, determines template gets used this.$el.append(projectview.render().el); } }); var projectview = backbone.view.extend({ tagname: "div", classname: "project-wrap", template: _.template($(this.options.projecttype).html()), // use this.options access value render: function() { this.$el.html(this.template(this.model.tojson())); return this; } });
when you're defining projectview
:
var projectview = backbone.view.extend({ //... template: _.template($(this.options.projecttype).html()), //... });
you're executing code (i.e. calling extend
) , in context, this
global object (aka window
in browser) , won't have options
property. if want use projecttype
option pass projectview
constructor, move template
assignment initialize
:
var projectview = backbone.view.extend({ tagname: "div", classname: "project-wrap", initialize: function() { this.template = _.template($(this.options.projecttype).html()); }, render: function() { this.$el.html(this.template(this.model.tojson())); return this; } });
this assumes projecttype
valid jquery selector, might want use '#' + this.options.projecttype
instead i'm not sure in projecttype
.
Comments
Post a Comment