I’m new to Backbone and would like some light shed on how I should approach this. I might be not using the right definitions here – sorry in advance!
Currently I have rows of elements that each have a button (I’ll refer to these buttons as button A). These rows are generated as Rails partial. The button opens a popup box that has some controls specific to that row of information. Code-wise, there is only one popup box that adjusts itself + information inside it each time any of the button A is clicked, and it is located in an index.html.erb file.
I want to turn these code into Backbone view object – but I don’t know how to make the pop up box view interact with multiple buttons.
Would a right way to approach this be create a pop-up box per button and let there be multiple pop-up views that communicate with each other? What would be the right approach for this?
Currently, none of these elements are backbone view object.
Thank you in advance.
I’m assuming:
JSFiddle example: http://jsfiddle.net/4J6fL/
The first step is to create a Backbone.View instance that is responsible for the functionality of your data list.
At this point we have a View which can be bound to any DOM element, giving it the above functionality.
eventsproperty is responsible for binding event callbacks to the view.thisin your callbacks, Backbone binds it to the View, not the event.this.$elrefers to the DOM element bound to our view. You’ll see how to do further on.Now that we have our view, we need to initialize and use it. This is done in the Backbone.Router, which acts as a controller:
Notice how we’re passing the element (
el) that will be bound to this view? This is what binds a DOM element to our view, allowing us to manipulate it from within the view.The next logical step would be to separate the popup code into another view, using events to signal when it should be updated. This would allow you to easily trigger the popup from other areas of your page.