Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is everyone still constructing parent-child views using the render method?

I dare call myself a backbone hacker. I know what the framework can do, and where its limitations are. I also have some experience with a few templating frameworks.

I've seen many tutorials where people explain how to create complex and nested views, and most of them construct it kinda partially using templates, and then within the render method of the parent view, in order to combine the templated child views

To me, this makes no sense why one should deal with the layout rendering, in the declarative code. Coming from Flex, I was taught to never do that. I always left the layout descriptions and variable bindings to the markup, and then the event handling to the declarative (View instance) code which uses this markup.

None of the templating frameworks I tested, however, allows the creation of complex markup, with nested views. One cannot really invoke a template from a template and thus instantiate a View object. This seems technically possible, especially using the data attributes, where we could specify type names.

Then, all the render method of the root level View class has to do is turn this template into HTML markup, then find out what the types of the child objects should be, create a child view instance for any of them, and keep further, in case those child objects should have child objects themselves. Every view is given a model context. Basically all the boilerplate steps that we deal with all the time, but automated at the Backbone.View level.

Anyone else thinking about this? Why does no one seem to be using this?

like image 367
user802232 Avatar asked Nov 05 '22 10:11

user802232


1 Answers

It should be noted that it is not necessary to use render at all and it is mainly reserved for re-rendering after changes to code has been made. You can bind views directly based on CSS selectors (see the docs for this).

Additionally there is a model binding extension for Backbone which greatly simplifies data-binding and reduces the 'manual' labor required. You might want to check it out.

http://github.com/derickbailey/backbone.modelbinding

Finally I will say this about rendering parent-child relationships. Do not call the DOM in a loop. This is incredibly inefficient and at least one reason people will build up parent-child relationships only in the parents render method. Having each child render itself using say jQuery will result in a lot of work for the browser (if you don't notice this in a modern browser try it in IE8).

like image 155
Chris Nicola Avatar answered Nov 10 '22 18:11

Chris Nicola