I found that everything will get complicated when you try to support server-side views and client-side views. The script you are describing here is a great example of this.
If at all possible, I would move the page rendering to the client side, which would greatly simplify your code.
If you want to save views processed by the server, I would probably execute the fetch () function in your collection after loading the page to get all objects from the server. You can then configure the ResultView initialization function to perform the following test.
ResultView = Backbone.View.extend initialize: (attributes) -> exisitingElement = $('result_' + attributes['id']) if exisitingElement? @el = exisitingElement @delegateEvents()
Then you change your template to a unique identifier.
<li id="result_{{id}}" class="result"> <h1> <a href="{{link}}">{{title}}</a> </h1> <p>{{snippet}}</p> </li>
Thus, ResultView will search for an existing item before rendering a new one to the page. Manually calling delegateEvents () after reassigning the @el property ensures that all events that you define will continue to work.
source share