I have two different ways of displaying my models on my site, and I handle them with different views of course.
However, I want to be able to connect these views somehow, such that when an event happens on one of the views on a specific model, it can also trigger an event on the other view.
For sake of simplicity, let's say that I have a collection and that I represent the collection with two views that generate identical ul
lists. (In reality, the views are of course different).
HTML
<ul class="view-a">
<li class="subview-a">Model 1</li>
<li class="subview-a">Model 2</li>
</ul>
<ul class="view-b">
<li class="subview-b">Model 1</li>
<li class="subview-b">Model 2</li>
</ul>
Backbone
viewA = Backbone.View.extend({
tagName: 'ul',
className: 'view-a',
});
viewB = Backbone.View.extend({
tagName: 'ul',
className: 'view-b',
});
subViewA = Backbone.View.extend({
tagName: 'li',
className: 'subview-a',
events: {
'mouseover':'over',
},
over: function() {
console.log('mouse over on A');
}
});
subViewB = Backbone.View.extend({
tagName: 'li',
className: 'subview-b',
events: {
'mouseover':'over',
},
over: function() {
console.log('mouse over on B');
},
});
You might ask: Why not have the same subview? In this example both sub-views are li
, but not in the actual implementation.
So how can I trigger the mouseover event on subview B
when hovering over subview A
, and vice-versa?