I want to call some function after html is rendered in all view. So, I want some event which is raised after backbone view is rendered. Is there any existing event or how do I make my own event using view extending?
2 Answers
3
votes
You should be able to do something like
var MyView = Backbone.View.extend(Backbone.Events, {
// usual code (template, etc.)
render: function(){
// code to render view
// trigger event
this.trigger("render:after", "View rendered !");
}
});
Then, to react to the event:
var view = new MyView(...);
view.listenTo(view, "render:after", function(msg){
alert(msg);
});
2
votes
You can do like this :
var render = Backbone.View.prototype.render;
Backbone.View.prototype.render = function() {
var rendered = render();
this.trigger("render", this);
return rendered;
}
So whenever a view is rendered the render event will be triggered. And you don't need to everytime you extend the Backbone.View
to add the this.trigger("render..