Hi,
Recently I was involved in a very large project that involved multiple teams developing on the front end.
We used Marionette as our framework and were very happy with it.
One issue we dealt with a lot was creating reusable UI components that would be consistent throughout the project.
Components could be very simple elements - like buttons, input fields, etc. - or very complex - like a color-picker.
Essentially, these components would be used in order to create views very simply. By reducing a view to a container that aggregates these components, and adds some specifics, it becomes easy to create new pages.
We wanted to avoid having our templates full of multiple CSS classes, (e.g <a class="my-btn btn btn-primary btn-large .../>).
We also wanted to avoid duplication of the same options passed to many jQuery UI widgets (e.g. datepicker options). Moreover, many jQuery widgets use callback methods, and not events, which was different from our MVC approach.
From previous projects, we saw those patterns explode to major Copy+Pasting and CSS/JS duplication/hacking.
So we decided to add another layer of abstraction.
Every component was simply a Marionette view which defined a strict interface. Each larger "page" view, would render multiple child component views for every component it needed to include.
This way, a button template was defined once, and all CSS classes were encapsulated there (even if those classes were Bootstrappy).
A jQuery datepicker was wrapped, and callbacks were only defined inside the DatePicker view. This DatePicker would trigger specific events to the parent "page" (with the useful "tiggers" hash) allowing for the non-event driven style to be in one specific place.
We took this a step further and extended the Marionette base View object to include a"uiComponents" hash, similar to the "ui" hash.
This reduced all instantiation, destroying and event binding to a simple declaration on the parent "page" view.
This approach worked great, however, it did add another layer of code and tests.
It introduced more code to write, albeit easier to maintain in the long run.
It demanded thinking of component as generic as possible in order to define them correctly.
I am now at the beginning of a new project, and I'm thinking of reusing this design pattern.
But before I start, I'd love to hear opinions about this approach.
Do you think it takes abstraction too far, especially for simple elements? Do you think it's worth it?
Do you think it adds unnecessary code?