here's the situation: When page is opened for the first time, it already has prepared DOM by server(php).
If user has javascript turned on, then i want to convert my page to web app(or whatever you call it). As soon as Javascript is initialized, Backbone fetches collection from server. The problem is, that some of these fetched items are already on page.
Now how can i mark those items which already are in the DOM? And how can i tie them up with the Backbone view?
Ok, i managed to do that like so:
Hooking up a Backbone.View to an existing DOM element is simple:
The view now handles the
#foo
element's events, and all the other View goodness. You shouldn't callview.render
. If you do, it will re-render the view to the element. This means that you can't define any necessary code in therender
method.As to how to find out which elements are already in the DOM, and how to find the corresponding element for each view - that's a bit more complicated to answer without knowing exactly how your data and html looks like. As a general advice, consider using
data-*
attributes to match up the elements.Let's say you have a DOM tree:
You could bind/render a model to the container like so: