I have a render method in Backbone that goes basically like this:
render: function () {
$.tmpl(this.template, attrs).appendTo(this.el);
return this;
},
which is called from a router action:
action: function () {
$('#container').empty();
$('#container').append(myView.render().el);
},
Now, I want to apply a plugin on label
elements inside this view. My first thought was to call the plugin inside render
:
render: function () {
$.tmpl(this.template, attrs).appendTo(this.el);
this.$('label').inFieldLabels();
return this;
},
but this doesn't work (I'm assuming this is because the element hasn't been added to the DOM yet). It does work if I call the plugin in the router action though:
action: function () {
$('#container').empty();
$('#container').append(myView.render().el);
myView.$('label').inFieldLabels();
},
I'd rather not do this, because the plugin is part of the view, not the router, so it doesn't make sense to be calling it inside the action. Is there a better way to do this?