0
votes

Is there any way I can find out the rendered view in backbone? I have 4 views Login, Contact, Home and About I would like to find out which view currently is rendered.

1
That's hard to answer without a little more detail on your setup. There is not an "active view" property or anything like that built in to Backbone, but you could build one yourself. Can you post some sample code showing how you create/navigate the views to provide context? - providencemac
The easiest way would be to just add a console.log to each of the views that's triggered in their initialize function. - EmptyArsenal

1 Answers

0
votes

Assuming you're rendering all the views into the same element (otherwise you could know what the view is from the element id), you might add a property like 'class' to the view when you create it. Then that property can be accessed through view.options.

For example:

var LoginView = Backbone.View.extend();

var loginView = new LoginView({ model: new Backbone.Model, el: 'body', class: 'login' });

loginView.render = function() { var content = 'login'; this.$el.html(content) };

loginView.render();

console.log(loginView.options.class) // 'login'

Obviously this is an oversimplified example but the general idea should work. More details or a code sample would help if you need a more specific answer.

If you also wanted to make sure the view is actually rendered, just write a method that checks if content of the view is what you expect it to be