The current home is indeed part of the global state of the application, so create a new model to store your global state of the application:
var AppState = Backbone.Model.extend({ }); var app_state = new AppState;
Then your HouseListElemView can respond to clicks by setting the value to app_state :
App.HouseListElemView = Backbone.View.extend({ //... events: { 'click': 'set_current_house' }, set_current_house: function() { // Presumably this view has a model that is the house in question... app_state.set('current_house', this.model.id); }, //... });
and then your MapView just listens for 'change:current_house' events from app_state :
App.MapView = Backbone.View.extend({ //... initialize: function() { _.bindAll(this, 'show_house'); app_state.on('change:current_house', this.show_house); }, show_house: function(m) { // 'm' is actually 'app_state' here so... console.log('Current house is now ', m.get('current_house')); }, //... });
Demo: http://jsfiddle.net/ambiguous/sXFLC/1/
You might want current_house be a real model, not just an id , but it's easy.
You can probably find all sorts of other uses for app_state after it appears. You can even add a bit of REST and AJAX and get consistency for the settings of your application to a large extent for free.
Events are the usual solution to every problem in Backbone, and you can create models for anything, you can even make temporary models strictly for gluing things together.
mu is too short
source share