JavaScript layout management for MVC/Backbone.js application? - javascript

My web application, written in Backbone.js, has what I call a number of "Modes".
Each mode describes which models and views that are currently active. By changing the mode parameter in the hash (#) I switch between modes and instantiate necessary Backbone models & views.
It seems logical to also describe the preferred layout and inject the container html code on a mode level.
Let's say the mode "PopulationPyramidMode" has a model and 3 Backbone views. The "HeaderView" should take up 30 pixels vertically. The "SidebarView" should take up 200 pixels horizontally and the "CanvasView" should fill moist of the screen and scale on resize events.
To complicate things further, the CanvasView holds a visualization which must be re-rendered on resize, i.e. the x/y-scales must be re-calculated.
So, what is the best approach to this?
1) Where should the layout logic go?
- On a separate layoutManager - and in that case how?
- Should each view describe its preferred size etc?
2) How do I inject the necessary HTML/CSS for my layouts and switch between them?
<!-- Layout 1 -->
<div id="header"></div>
<div id="sidebar"></div>
<div id="canvas"></div>
<!-- Layout 2 -->
<div id="header"></div>
<div id="canvas"></div>
Thanks for any specific hints and also on JavaScript layouts in general!

Just thinking out loud here. It sounds like you could have one view which manages the layout, but is capable of morphing the layout as needed. The question is: what triggers the layout change?
You might have a controller which is equipped with the routes and actions for these different modes. This controller could either hold an instance to this layout view and call a parameterized refresh function. Alternatively, the controller could trigger an event with the mode change and the view could be set up to listen for it. Once received, the view would call its refresh function.

My opinion regarding building such applications would be to use the straightforward observer pattern.
Design your controller in a way that it simply dispatches particular events when the hashtag changes. Have an event map in the controller,that acts as a reference table between events to be dispatched and the urls, better still, you can follow a particular convention regarding the choice of hash parts eg. everytime user navigates to url mydomain.com/#part1/part2 the controller dispatches an event 'evt_part1_part2'. You can use a common dispatch point (which can be a dom element, or a javascript object particularly being used for this very purpose).
Now all your views can listen to this common dispatch point, and if the event matches the one that this view is looking for, you can create the respective environment accordingly through that view.
This approach definitely helps in compartmentalization of views, leading of more modular and maintainable code.

Related

Component based clientlibs AEM

Is it better to split up clientlibs by components if it means more calls to the server?
I.e. using
<%#taglib prefix="ui" uri="http://www.adobe.com/taglibs/granite/ui/1.0" %>
<ui:includeClientLib categories="mqd.component.accordion" />
in the <component>.jsp instead of compiling all the CSS in a single stylesheet.
From what I know, this is more of a decision based your use case, there is no one approach which fits all the scenarios -
Loading CSS at component level
When you load CSS at the component level, it is not available in the HEAD section when the page rendering process kicks off. It will only render your component CSS when it encounters it somewhere in the body tag.
Conditionally loading CSS based on the component is not available by default, you would have to write custom logic to achieve this.
From this post,
One way to achieve this is to intercept that behaviour. Use a filter
and buffer all data written to the output buffer in memory. Then you
can render safely all components and if you encounter your special
component you can set a flag in the request attributes. The filter can
then check for these attributes, change the buffer accordingly and
then send everything out. That approach is a bit risky, because it
can consume a lot of memory. And it changes the rendering performance and behaviour of your page. But it might be worth a try.
Also, with component level CSS, you would have to ensure the styles
for a component don't affect styles for another component, i.e. you
would have to use narrow selectors to do this and ensure you don't
break anything else in the process.
Also, with component level CSS, you would have to ensure the styles for a component don't affect styles for another component, i.e. you would have to use narrow selectors to do this and ensure you don't break anything else in the process.
Other approaches
Using page components - If you have a component which has a lot of styles and you don't want this to get loaded on every other page, you can look at using page components(different templates) to achieve this. Each page component can load a different group of clientslibs based on its use.
Using deferred clientlibs - If your layout constantly changes and you’re worried about how big your clientlibs file has become, deferred clientlibs might be a better option. Example from the link listed below -
… [Navigation component logic]
<ici:js-defer>
<cq:includeClientLib js=”icidigital.components.navigation”/>
</ici:js-defer>
[Navigation component end]
… [Sitemap component logic]
<ici:js-defer>
<cq:includeClientLib js=”icidigital.components.siteMap”/>
</ici:js-defer>
[Sitemap component end]
becomes…
<div class=”footer” />
<script type=”text/javascript” src=”path/to/programmatically/combined/deferred/clientlib.js”></script>
</div>
Whatever approach you take, ensure caching, page load times, maintenance, performance, etc are taken into account.
Further read
Best approaches to clientlibs in AEM
CSS best practices in clientlibs

Angular UI-router nested views capabilities

I am trying to learn and understand the capabilities of UI-router with Angular 1.3.15.
I am trying to set up an application which has many views that have a header and footer directive. It also has a smaller number of views that do not need this setup, with the loaded view taking up the entire page.
Therefore, it seems I should handle this divergence "one level down", as in my diagram below. In the past, I have worked on ui-router apps with the index.html coded with the header/footer directives and a single ui-view for the other pages to load into. This time I am trying to get it correct form the start. Opinions and advice welcome.
I'm not sure what you want to know.
Yeah, you should handle the difference in templates the way you suggested: the root template should contain only the elements which appear on all states. Elements which appear on some states should go on those states templates, in the template of a parent state (if it makes sense), or in directives that you reuse in the various templates.
Instead of directives, you might want to use named views if your templates have some peatures in common, but the differences between them are not inside a single DOM element. For example, maybe all your pages have a small toolbar on top that always has some buttons, but other buttons depend on the state you are in. You can place that constant part of the toolbar in the root template, together with a <div ui-view="toolbar"></div>. The states would then define a view named toolbar with a template with the buttons they want to add.
You could make a directive for that toolbar with all the global buttons in its template and use <ng-transclude> to add the custom buttons at each state's template, but using named views seems cleaner.

Backbone MVP - Who attaches the view?

So I am writing a Backbone.js application.
I have opted to use the MVP pattern with Passive Views.
Now my arch so far is:
Application
Page Presenter
Layout View
Header Presenter
Layout View
Logo View
User View
Content Presenter
Layout View
Footer Presenter
Layout View
Now I am very new to the concept of MVP (had a lot of experience with MVC).
So say I want to render the header's layout and then attach it to the Page Presenter's layout view, who's responsibility is it to do so?
Most often, I see simple HTML/Image elements contained in a view template that is brought in via Handlebars, Underscore templating, or the like to the view. This allows you to keep your javascript and html separated. Absent that, HTML would live directly in the view.
The examples in this article make a common pattern for templates in MVP pretty clear.
I prefer to go one step further in my apps, starting with a common template in index for things like logos, main page layout, etc....then drawing my views in an element of that common template assisted by specialized, smaller, templates-per-view that contain specific html content to make the view work. The downside is that some template-per-views can be empty files, but that is a very rare occurance. It's also quite clear to the rest of the team where an element is going to be if they need to edit it.

Angularjs Partial View vs Directive

Our SPA has 2 distinctive top level views. To compare it is like windows file explorer showing tree view on one side and content details on other side. For these top level views, we are considering to have 2 partial views. Other alternative is to pack these views as directives. Our initial thoughts are going toward partial views, because these are quite larger blocks of functionalties and each view can have multiple controllers. Any experience/thoughts on similar lines would help us decide. Just a note we communicate between these views using eventing mechanism.
We do not intend to reuses these views. Specifically, are there any issues going partial views? Like performance, maintainability, etc.
I'm not sure I'm understanding the problem here, so sorry if I say something wrong (also sorry for my english).
What you need are 2 views; if 'inside' those views you use a directive or not, it's another thing.
The only thing I'm pretty sure is that those 2 views need to have they're own scope.
To me it seems a lot like a 'navigation menu' vs. 'view' kind of problem (only that the navigation part is gonna be some sort of tree-view), so the solution should be similar:
the 'normal' ngView (your 'details' side);
a div with it's own controller (and it's own scope).
Something like:
<nav ng-controller="treeViewController()">
<!-- here we use a directive, for example -->
<tree-view ng-model="tree"></tree-view>
</nav>
<div ng-view></div>
Then the best way to make them communicate is probably a custom service.
In case I misunderstood your problem, sorry in advance.

Backbone Newbie: Approaching layouts

I have a backbone app (linked to rails) that currently looks a lot like a standard RESTful resource.
I currently have a 'new_post' link on my index page called via the following:
Backbonedemo.Views.PostsIndex extends Backbone.View
# ...
events: ->
'click #new_post' : "newPost"
newPost: ->
Backbone.history.navigate("/posts/new", true)
# ...
I'd like to include that on each of the backbone RESTful pages I have (index, show, edit, etc) and I'm wondering how to do it.
My initial plan was to place the #new_post html in the (non-backbone) parent rails template, but I couldn't figure out where to bind an event from each backbone view.
Alternatively, I guess I could throw in some sort of layouts in the (eco) templating system.
Last - and least desirable - is to set up a click #new_post event in each view, and render it in each backbone template. Blergh.
So, what's the most elegant way of approaching this? Is there a place to elegantly place event bindings across multiple templates? Ie the router or something, and if so, how?
Many thanks
If on click the only action is to change the route of the page, you can simply make the #new_post a link to /post/new and skip making any programmatic changes.

Categories

Resources