derickbailey/backbone.modelbinding — pull request #67 by theironcook (2012-02-16 17:52:23+00:00)
Title: Enable Nested Model Binding, Restrict Field Binding

Hi Derick,

I've added an optional binding configuration option for a callback named 'shouldBindToEl' which takes an element as an argument and returns true or false if the element should be bound to the view’s model. This allows views to contain nested views that are independently bound to nested models. Views can define which sub elements are bound to the view’s model allowing nested view elements to be bound to nested models. This callback also allows a view to specify that certain sub elements should be ignored by the model binders.

I work on a large app with about 20 other developers. We like the 2 way model binding plugin you've written - it's great but it doesn't work well with nested models / nested views. Probably about 1/2 of our top level views have nested models (a reusable Address Model + View is a good example). We run into problems where the nested views update parent models unless we are careful on the order of binding which is not ideal. Also there are times when we want a particular field ignored by the model binder.

What we needed was a way to define the boundaries for the model binder - exactly which elements are to participate in the model binding. The callback 'shouldBindToEl' gives us this flexibility - the bound view has to define what the boundaries are and the model binders ask for this boundary.

I added 64 new unit tests for this functionality. 32 use the id attribute and 32 use the name attribute - we try to use the name attribute (especially with nested models where the same view definition is repeated and the id will be repeated) so I wanted to make sure there were no issues binding to name.

I've done some performance testing with a page with 500 outer view elements and 100 inner view elements and checked the binding from the view to the model and the model to the view and there is not a difference is execution speed with the enhancement.