I'm trying to include an optional argument in my Backbone view's render function, like this:
var AppView = Backbone.View.extend({
el: '#app',
initialize: function(){
_.bindAll(this,'render');
this.listenTo(app.collection.sellables,'reset', this.render);
},
render: function(sellableId){
if(typeof sellableId == "undefined"){
var sellable = app.collection.sellables.first();
}
else{
var sellable = app.collection.sellables.get(sellableId);
}
var view = new SellableView({model: sellable});
this.$('#sellables').append(view.render().el);
},
});
The first time this view renders, it occurs because the app.collection.sellables collection gets reset after receiving some data from the server. When this reset occurs, the view's render function gets called, but instead of being called with sellableId as undefined, the sellableId is equal to the app.collection.sellables variable, which is causing errors. To clarify, the view's render function is being called because of this:
app.collection.sellables.reset(data.skus);
Why would my render function be receiving the collection as an argument rather than the argument being undefined?
Also, if I render the view manually, it works correctly and the sellableId argument is undefined as expected:
var app.view.app = new AppView;
app.view.app.render();