I have three different ways to initialize and render a view and its subviews, and each one of them has different problems. I’m curious to know if there is a better way that solves all of the problems:
Scenario One:
Initialize the children in the parent’s initialize function. This way, not everything gets stuck in render so that there is less blocking on rendering.
initialize : function () {
//parent init stuff
this.child = new Child();
},
render : function () {
this.$el.html(this.template());
this.child.render().appendTo(this.$('.container-placeholder');
}
The problems:
-
The biggest problem is that calling render on the parent for a second time will remove all of the childs event bindings. (This is because of how jQuery’s
$.html()works.) This could be mitigated by callingthis.child.delegateEvents().render().appendTo(this.$el);instead, but then the first, and the most often case, you’re doing more work unnecessarily. -
By appending the children, you force the render function to have knowledge of the parents DOM structure so that you get the ordering you want. Which means changing a template might require updating a view’s render function.
Scenario Two:
Initialize the children in the parent’s initialize() still, but instead of appending, use setElement().delegateEvents() to set the child to an element in the parents template.
initialize : function () {
//parent init stuff
this.child = new Child();
},
render : function () {
this.$el.html(this.template());
this.child.setElement(this.$('.placeholder-element')).delegateEvents().render();
}
Problems:
- This makes the
delegateEvents()necessary now, which is a slight negative over it only being necessary on subsequent calls in the first scenario.
Scenario Three:
Initialize the children in the parent’s render() method instead.
initialize : function () {
//parent init stuff
},
render : function () {
this.$el.html(this.template());
this.child = new Child();
this.child.appendTo($.('.container-placeholder').render();
}
Problems:
-
This means that the render function now has to be tied down with all of the initialization logic as well.
-
If I edit the state of one of the child views, and then call render on the parent, a completely new child will be made and all of its current state will be lost. Which also seems like it could get dicey for memory leaks.
Really curious to get your guys’ take on this. Which scenario would you use? or is there a fourth magical one that solves all of these problems?
Have you ever kept track of a rendered state for a View? Say a renderedBefore flag? Seems really janky.
This is a great question. Backbone is great because of the lack of assumptions it makes, but it does mean you have to (decide how to) implement things like this yourself. After looking through my own stuff, I find that I (kind of) use a mix of scenario 1 and scenario 2. I don’t think a 4th magical scenario exists because, simply enough, everything you do in scenario 1 & 2 must be done.
I think it’d be easiest to explain how I like to handle it with an example. Say I have this simple page broken into the specified views:
Say the HTML is, after being rendered, something like this:
Hopefully it’s pretty obvious how the HTML matches up with the diagram.
The
ParentViewholds 2 child views,InfoViewandPhoneListViewas well as a few extra divs, one of which,#name, needs to be set at some point.PhoneListViewholds child views of its own, an array ofPhoneViewentries.So on to your actual question. I handle initialization and rendering differently based on the view type. I break my views into two types,
Parentviews andChildviews.The difference between them is simple,
Parentviews hold child views whileChildviews do not. So in my example,ParentViewandPhoneListViewareParentviews, whileInfoViewand thePhoneViewentries areChildviews.Like I mentioned before, the biggest difference between these two categories is when they’re allowed to render. In a perfect world, I want
Parentviews to only ever render once. It is up to their child views to handle any re-rendering when the model(s) change.Childviews, on the other hand, I allow to re-render anytime they need since they don’t have any other views relying upon them.In a little more detail, for
Parentviews I like myinitializefunctions to do a few things:InfoViewwould be assigned#info).Step 1 is pretty self explanatory.
Step 2, the rendering, is done so that any elements the child views rely on already exist before I try to assign them. By doing this, I know all child
eventswill be correctly set, and I can re-render their blocks as many times as I want without worrying about having to re-delegate anything. I do not actuallyrenderany child views here, I allow them to do that within their owninitialization.Steps 3 and 4 are actually handled at the same time as I pass
elin while creating the child view. I like to pass an element in here as I feel the parent should determine where in its own view the child is allowed to put its content.For rendering, I try to keep it pretty simple for
Parentviews. I want therenderfunction to do nothing more than render the parent view. No event delegation, no rendering of child views, nothing. Just a simple render.Sometimes this doesn’t always work though. For instance in my example above, the
#nameelement will need to be updated any time the name within the model changes. However, this block is part of theParentViewtemplate and not handled by a dedicatedChildview, so I work around that. I will create some sort ofsubRenderfunction that only replaces the content of the#nameelement, and not have to trash the whole#parentelement. This may seem like a hack, but I’ve really found it works better than having to worry about re-rendering the whole DOM and reattaching elements and such. If I really wanted to make it clean, I’d create a newChildview (similar to theInfoView) that would handle the#nameblock.Now for
Childviews, theinitializationis pretty similar toParentviews, just without the creation of any furtherChildviews. So:Childview rendering is also very simple, just render and set the content of myel. Again, no messing with delegation or anything like that.Here is some example code of what my
ParentViewmay look like:You can see my implementation of
subRenderhere. By having changes bound tosubRenderinstead ofrender, I don’t have to worry about blasting away and rebuilding the whole block.Here’s example code for the
InfoViewblock:The binds are the important part here. By binding to my model, I never have to worry about manually calling
rendermyself. If the model changes, this block will re-render itself without affecting any other views.The
PhoneListViewwill be similar to theParentView, you’ll just need a little more logic in both yourinitializationandrenderfunctions to handle collections. How you handle the collection is really up to you, but you’ll at least need to be listening to the collection events and deciding how you want to render (append/remove, or just re-render the whole block). I personally like to append new views and remove old ones, not re-render the whole view.The
PhoneViewwill be almost identical to theInfoView, only listening to the model changes it cares about.Hopefully this has helped a little, please let me know if anything is confusing or not detailed enough.