'Backbone view triggers a global event
In a nice world, backbone view notifies underlying model, model triggers the event. View updates itself as a response to event.
Is it legit to if view triggers an event and another view responds to this event. underlying model might even have no clue on this?
Assume I have a cart is a collection, now a I have a view call it glimpse view with item count and total cost which has a link to view the verbose content of cart.
So user clicks the link the view triggers the event however this get caught by controller or any other party but the model.
Leaving the underlying model out of game, is it? legit?
Solution 1:[1]
The nice thing about Backbone is its flexibility, and it doesn't require you to strictly adhere to a specific architecture.
So it's no problem for one view to trigger an event, and another view directly responds to this event without changing anything on the model.
The question that needs asked, though, is what's the cleanest way to implement the architecture.
In my opinion on the situation you described, I would use a global event object for the 2 views to communicate with each other.
Here's some example code
var globalEvents = {};
_.extend(globalEvents, Backbone.Events);
var GlimpseView = Backbone.View.extend({
events: {
'click a.see-verbose': 'onSeeVerboseClick'
},
onSeeMoreClick: function() {
globalEvents.trigger('seeVerbose', this.model);
}
});
var VerboseView = Backbone.View.extend({
initiliaze: function() {
globalEvents.on('seeVerbose', onSeeVerbose, this);
},
onSeeVerbose: function(model) {
// code to render the verbose view
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
